public int strStr(String haystack, String needle) {
char[] s = haystack.toCharArray();
char[] t = needle.toCharArray();
int[] next = new int[t.length];
for (int h = 1, q = 0; h < t.length; h++) {
while (q > 0 && t[q] != t[h]) {
q = next[q - 1];
}
if (t[q] == t[h]) {
q++;
}
next[h] = q;
}
for (int tt = 0, ss = 0; ss < s.length; ss++) {
while (tt > 0 && t[tt] != s[ss]) {
tt = next[tt - 1];
}
if (t[tt] == s[ss]) {
tt++;
}
if (tt == t.length) {
return ss - tt + 1;
}
}
return -1;
}
07-08
1655
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-08
121
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)