Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
public class Solution {
public int strStr(String haystack, String needle) {
if(needle.length() == 0) {
return 0;
} else if(haystack.length() < 1) {
return -1;
} else if(haystack.length() < needle.length()) {
return -1;
}
int hLength = haystack.length(), nLength = needle.length();
for(int i = 0; i <= hLength - nLength; i++) {
for(int j = 0, k = i; j < needle.length(); j++, k++) {
if(haystack.charAt(k) != needle.charAt(j)) {
break;
} else if(j == needle.length()- 1){
return i;
}
}
}
return -1;
}
}