public static int strStr(String haystack, String needle) {
int needleLen = needle.length();
if(needleLen == 0) {
return 0;
}
int[] next = new int[needleLen];
next[0] = -1;
int j = -1;
int i = 0;
/*getNext*/
while (i < needleLen - 1) {
if (j == -1 || needle.charAt(j) == needle.charAt(i)) {
j++;
i++;
next[i] = j;
} else {
j = next[j];
}
}
i = 0;
j = 0;
/*matching*/
while (i < haystack.length() && j < needle.length()) {
if (j == -1 || haystack.charAt(i) == needle.charAt(j)) {
i++;
j++;
} else {
j = next[j];
}
}
if (j == needle.length()) {
return i - j;
}
return -1;
}