1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| func strStr(haystack string, needle string) int { next, j := make([]int, len(needle)), 0 getPrefix(next, needle) for i := 0; i < len(haystack); i++ { for j > 0 && haystack[i] != needle[j] { j = next[j-1] } if haystack[i] == needle[j] { j++ } if j == len(needle) { return i - j + 1 } } return -1 } func getPrefix(next []int, pattern string) { j := 0 next[0] = 0 for i := 1; i < len(pattern); i++ { for j > 0 && pattern[i] != pattern[j] { j = next[j-1] } if pattern[i] == pattern[j] { j++ } next[i] = j } }
|