-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution2.js
88 lines (82 loc) · 1.59 KB
/
solution2.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* https://leetcode-cn.com/problems/implement-strstr/
*
* 28. 实现strStr()
*
* Easy
*
* KMP
*
* http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html
*
*
* O(m+n)
*
* 88.51%
* 7.95%
*/
const strStr = (haystack, needle) => {
const partial = getPartial(needle)
const l1 = haystack.length
const l2 = needle.length
let x = 0
let y = 0
while (x < l1 && y < l2) {
if (y === 0 && haystack[x] !== needle[y]) {
x++
continue
}
if (haystack[x] === needle[y]) {
x++
y++
continue
}
// 根据部分匹配表将 needle 与当前匹配的位置对齐
y = partial[y - 1]
}
if (y === l2) {
return x - l2
}
return -1
}
// 部分匹配值
function getPartial(str) {
const max = str.length
const partial = Array(max).fill(0)
let start = 0
let end = 1
while (end < max) {
if (start === 0 && str[start] !== str[end]) {
partial[end] = 0
end++
continue
}
if (str[start] === str[end]) {
partial[end] = start + 1
start++
end++
continue
}
if (str[start] !== str[end]) {
// 回退
let len = 0
let tempEnd = end
while (start >= 0) {
if (str[start] !== str[tempEnd]) {
len = 0
tempEnd = end
} else {
len++
tempEnd--
}
start--
}
partial[end++] = len
start += len + 1
}
}
return partial
}
console.log(getPartial('abcabbd')) // 0001200
console.log(getPartial('aabaaa')) // 010122
console.log(strStr('abcabbcabbd', 'abcabbd'))