mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Add knuth.
This commit is contained in:
parent
9081de3ded
commit
4afc8c759a
@ -41,10 +41,10 @@
|
||||
* **String**
|
||||
* [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
|
||||
* [Hamming Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
|
||||
* Knuth Morris Pratt
|
||||
* [Knuth–Morris–Pratt algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/knuth-morris-pratt) - substring search
|
||||
* Rabin Karp
|
||||
* Longest common subsequence
|
||||
* longest common substring
|
||||
* Rabin Karp
|
||||
* **Search**
|
||||
* [Binary Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/binary-search)
|
||||
* **Sorting**
|
||||
|
19
src/algorithms/string/knuth-morris-pratt/README.md
Normal file
19
src/algorithms/string/knuth-morris-pratt/README.md
Normal file
@ -0,0 +1,19 @@
|
||||
# Knuth–Morris–Pratt Algorithm
|
||||
|
||||
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.
|
||||
|
||||
## Complexity
|
||||
|
||||
- **Time:** `O(|W| + |T|)` (much faster comparing to trivial `O(|W| * |T|)`)
|
||||
- **Space:** `O(|W|)`
|
||||
|
||||
## References
|
||||
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm)
|
||||
- [YouTube](https://www.youtube.com/watch?v=GTJr8OvyEVQ)
|
@ -0,0 +1,12 @@
|
||||
import knuthMorrisPratt from '../knuthMorrisPratt';
|
||||
|
||||
describe('knuthMorrisPratt', () => {
|
||||
it('should find word position in given text', () => {
|
||||
expect(knuthMorrisPratt('abcbcglx', 'abca')).toBe(-1);
|
||||
expect(knuthMorrisPratt('abcbcglx', 'bcgl')).toBe(3);
|
||||
expect(knuthMorrisPratt('abcxabcdabxabcdabcdabcy', 'abcdabcy')).toBe(15);
|
||||
expect(knuthMorrisPratt('abcxabcdabxabcdabcdabcy', 'abcdabca')).toBe(-1);
|
||||
expect(knuthMorrisPratt('abcxabcdabxaabcdabcabcdabcdabcy', 'abcdabca')).toBe(12);
|
||||
expect(knuthMorrisPratt('abcxabcdabxaabaabaaaabcdabcdabcy', 'aabaabaaa')).toBe(11);
|
||||
});
|
||||
});
|
55
src/algorithms/string/knuth-morris-pratt/knuthMorrisPratt.js
Normal file
55
src/algorithms/string/knuth-morris-pratt/knuthMorrisPratt.js
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @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}
|
||||
*/
|
||||
export default function knuthMorrisPratt(text, word) {
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user