-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path28.Implement-strStr().go
63 lines (50 loc) · 982 Bytes
/
28.Implement-strStr().go
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// https://leetcode.com/problems/remove-duplicates-from-sorted-array/
//
// algorithms
// Easy (32.04%)
// Total Accepted: 430,978
// Total Submissions: 1,345,004
// beats 100.0% of golang submissions
package leetcode
func strStr(haystack string, needle string) int {
if needle == "" || haystack == needle {
return 0
}
return kmp(haystack, needle)
}
func kmp(source, target string) int {
sourceLen, targetLen := len(source), len(target)
if targetLen > sourceLen {
return -1
}
next := genNext(target)
i, j := 0, 0
for i < sourceLen && j < targetLen {
if j == -1 || source[i] == target[j] {
i++
j++
} else {
j = next[j]
}
}
if j == targetLen {
return i - targetLen
}
return -1
}
func genNext(target string) []int {
length := len(target)
next := make([]int, length)
next[0] = -1
i, k := 0, -1
for i < length-1 {
if k == -1 || target[i] == target[k] {
k++
i++
next[i] = k
} else {
k = next[k]
}
}
return next
}