-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathkmp.js
55 lines (50 loc) · 1.15 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 获取部分匹配表
*/
function getPartMatchTable(str) {
if (!str) {
return [];
}
const matchTable = [];
for (let i = 0; i < str.length; i++) {
let prefix = '';
let suffix = '';
const partStr = str.slice(0, i + 1);
for (let j = 0; j < i; j++) {
prefix = partStr.slice(0, j + 1);
suffix = partStr.slice(-j - 1);
if (prefix === suffix) {
matchTable[i] = prefix.length;
}
}
matchTable[i] = matchTable[i] || 0;
}
return matchTable;
}
/**
* 求子串的索引
*/
export default function kmp(sourceStr, subStr) {
const matchTable = getPartMatchTable(subStr);
for (let i = 0; i < sourceStr.length; i++) {
for (let j = 0; j < subStr.length; j++) {
if (sourceStr[i + j] === subStr[j]) {
// the last item
if (j === subStr.length - 1) {
return i;
}
}
else {
if (j === 0) {
break;
}
else {
// 移动位数 = 已匹配的字符数 - 对应的部分匹配值
// 这里需要先减 1,因为等下会加 1
i += (j + 1) - matchTable[j] - 1;
}
}
}
}
return -1;
}