-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmp.js
42 lines (41 loc) · 840 Bytes
/
kmp.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
/**
* KMP算法
*/
function fn (s, t) {
let i = 0,
j = 0,
next = getNext(t);
// console.
while (i < s.length && j < t.length) {
if (s[i] == t[j]) {
i++;
j++;
} else {
j = next[j];
}
}
return j >= t.length ? i-j : -1;
}
/**
* 根据输入值t,得到pwt数组
* 根据数组推算得到next数组
*/
function getNext (t) {
t = t.split('');
let next = [];
let i = 0,
j = 0;
while (i < t.length) {
if (i == 0) next[i++] = 0;
else if (t[i] == t[j]) next[i++] = (j++)+1;
else {
next[i++] = 0;
j = 0;
}
}
next.unshift(-1);
next.pop();
return next;
}
console.log(fn('ababababca','ac')) // => -1
console.log(fn('ababababca','abababca')) // => 2