-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringSearch.js
88 lines (76 loc) · 2.48 KB
/
stringSearch.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
/////////////////////////////////////////////////////////////////////////////////
// Loop over the longer string, loop over the shorter string, if the characters
// don't match, break out of the inner loop, if the characters do match, keep
// going... If you complete the inner loop and find a match, increment the count
// of matches, return the count.
/////////////////////////////////////////////////////////////////////////////////
function naiveSearch(long, short) {
var count = 0;
for (var i = 0; i < long.length; i++) {
for (var j = 0; j < short.length; j++) {
if (short[j] !== long[i + j]) break;
if (j === short.length - 1) count++;
}
}
return count;
}
naiveSearch('lorie loled', 'lol');
/////////////////////////////////////////////////////////////////////////////////
// The Knuth–Morris–Pratt string searching algorithm (or KMP algorithm) searches
// for occurrences of a "word" `W` within a main "text string" `T` by employing
// the observation that when a mismatch occurs, the word itself embodies
// sufficient information to determine where the next match could begin, thus
// bypassing re-examination of previously matched characters.
/////////////////////////////////////////////////////////////////////////////////
/**
* @see https://www.youtube.com/watch?v=GTJr8OvyEVQ
* @param {string} word
* @return {number[]}
*/
function buildPatternTable(word) {
const patternTable = [0];
let prefixIndex = 0;
let suffixIndex = 1;
while (suffixIndex < word.length) {
if (word[prefixIndex] === word[suffixIndex]) {
patternTable[suffixIndex] = prefixIndex + 1;
suffixIndex += 1;
prefixIndex += 1;
} else if (prefixIndex === 0) {
patternTable[suffixIndex] = 0;
suffixIndex += 1;
} else {
prefixIndex = patternTable[prefixIndex - 1];
}
}
return patternTable;
}
/**
* @param {string} text
* @param {string} word
* @return {number}
*/
function knuthMorrisPratt(text, word) {
if (word.length === 0) {
return 0;
}
let textIndex = 0;
let wordIndex = 0;
const patternTable = buildPatternTable(word);
while (textIndex < text.length) {
if (text[textIndex] === word[wordIndex]) {
// We've found a match.
if (wordIndex === word.length - 1) {
return textIndex - word.length + 1;
}
wordIndex += 1;
textIndex += 1;
} else if (wordIndex > 0) {
wordIndex = patternTable[wordIndex - 1];
} else {
wordIndex = 0;
textIndex += 1;
}
}
return -1;
}