basically, we need to implement indexOf()
so this problem is actually a string matching problem.
we met many of such string pattern matching algorithm.
a very simple one, but you did a pretty shit job as always.
class Solution {
public int strStr(String haystack, String needle) {
if (needle == null || needle.length() == 0) return 0; //the most connered case should be in the first place, and it matters
if (haystack == null || haystack.length() == 0) return -1;
if (haystack.length() < needle.length()) return -1;
int i = 0;
int j = 0;
while (i < haystack.length()) {
int start = i;
while (i < haystack.length() && j < needle.length() && haystack.charAt(i) == needle.charAt(j)) {
if (j == needle.length() - 1) {
return i - needle.length() + 1;
}
i++;
j++;
}
i = start + 1;
j = 0;
}
return -1;
}
}