-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathKMPAlgorithm.js
58 lines (55 loc) · 1.56 KB
/
KMPAlgorithm.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
/**
* @author Anirudh Sharma
*
* Given a text t[0..n-1] and a pattern p[0..m-1], write a function search(char p[], char t[])
* that prints all occurrences of p[] in t[]. You may assume that n > m.
*/
const search = (pattern, text) => {
// Prepare the pi table of prefix-suffix table
const lps = findLPS(pattern);
// Indices to move through text and pattern
// respectively
let textIndex = 0;
let patternIndex = 0;
// Loop until we reach to the end
while (textIndex < text.length && patternIndex < pattern.length) {
// If the characters match
if (text.charAt(textIndex) === pattern.charAt(patternIndex)) {
textIndex++;
patternIndex++;
} else {
if (patternIndex !== 0) {
patternIndex = lps[patternIndex - 1];
} else {
textIndex++;
}
}
}
return patternIndex === pattern.length;
};
const findLPS = (pattern) => {
const lps = [];
// Index for checking the pattern
let index = 0;
for (let i = 1; i < pattern.length;) {
if (pattern.charAt(i) === pattern.charAt(index)) {
lps.push(index + 1);
index++;
i++;
} else {
if (index != 0) {
index = lps[index - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
};
const main = () => {
let text = "abcxabcdabcdabcy";
let pattern = "abcdabcy";
console.log(`Does pattern exist? ${search(pattern, text)}`);
};
main();