Implement strStr() Java
来源:互联网 发布:浪潮数据库服务器 编辑:程序博客网 时间:2024/06/05 05:51
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
Goal: Determine whether needle is substring of haystack or not &
return the substring(i) i is first occurrence of needle in haystack
Idea: Use KMP algorithm is an optimal Approach to solve this problem
but since there is no time complexity requirement in problem.
Approach:
we can use brute force approach instead.
Check match in haystack with needle every time in lengt of needle using
nested for loop + boolean variable as a checker to skip unmatched
Time Complexity: O(n*m)
Space: O(1)
public class Solution { public String strStr(String haystack, String needle) { if(needle.length()==0) return haystack; int hLen=haystack.length(); int nLen=needle.length(); if(nLen>hLen){ return null;} for(int i=0;i<=hLen-nLen;i++){ boolean checker=true; //check the subString in length of needle for(int j=0;j<nLen;j++){ //skip it if they are unmatched if(haystack.charAt(i+j)!=needle.charAt(j)){ checker=false; break; } } if(checker){ return haystack.substring(i); } } return null; }}
0 0
- [Leetcode] Implement strStr() (Java)
- Implement strStr() Java
- Implement strStr() (Java)
- [LeetCode][Java] Implement strStr()
- (java)Implement strStr()
- LeetCode : Implement strStr() [java]
- 28. Implement strStr() java
- LeetCode – Implement strStr() (Java)
- LeetCode 28 : Implement strStr() (Java)
- 28. Implement strStr()-leetcode-java
- [LeetCode-Java]28. Implement strStr()
- [LeetCode] 28. Implement strStr() java
- [leetcode]28. Implement strStr()(Java)
- LeetCode-Easy-Java-Implement strStr()
- 28.Implement strStr() leetcode java
- Implement strStr()
- Implement strStr()
- Implement strStr()
- 根据PPC反汇编代码分析堆栈内容
- [杂谈]脚本语言在游戏发展中的作用
- [Python]networkx入门
- python遇到‘\u’开头的unicode编码
- 快速排序
- Implement strStr() Java
- 收集的linux远程ssh连接putty失败解决办法!
- Exception from container-launch: org.apache.hadoop.util.Shell$ExitCodeException
- hdu1045
- jquery.fn.extend与jquery.extend
- UIActivityIndicatorView
- 工厂方法——Factory Method
- 三个小招式,教你优雅地对付霸占你电脑的熊孩子
- PHP安装包TS和NTS的区别