mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 15:11:16 +08:00
Use '===' for double check string comparision in RabinKarp.
This commit is contained in:
parent
d303d83673
commit
e7d22b4883
@ -1,29 +1,5 @@
|
|||||||
import PolynomialHash from '../../cryptography/polynomial-hash/PolynomialHash';
|
import PolynomialHash from '../../cryptography/polynomial-hash/PolynomialHash';
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if two strings are equal.
|
|
||||||
*
|
|
||||||
* We may simply compare (string1 === string2) but for the
|
|
||||||
* purpose of analyzing algorithm time complexity let's do
|
|
||||||
* it character by character.
|
|
||||||
*
|
|
||||||
* @param {string} string1
|
|
||||||
* @param {string} string2
|
|
||||||
*/
|
|
||||||
function stringsAreEqual(string1, string2) {
|
|
||||||
if (string1.length !== string2.length) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let charIndex = 0; charIndex < string1.length; charIndex += 1) {
|
|
||||||
if (string1[charIndex] !== string2[charIndex]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} text - Text that may contain the searchable word.
|
* @param {string} text - Text that may contain the searchable word.
|
||||||
* @param {string} word - Word that is being searched in text.
|
* @param {string} word - Word that is being searched in text.
|
||||||
@ -52,10 +28,11 @@ export default function rabinKarp(text, word) {
|
|||||||
prevFrame = currentFrame;
|
prevFrame = currentFrame;
|
||||||
|
|
||||||
// Compare the hash of current substring and seeking string.
|
// Compare the hash of current substring and seeking string.
|
||||||
// In case if hashes match let's check substring char by char.
|
// In case if hashes match let's make sure that substrings are equal.
|
||||||
|
// In case of hash collision the strings may not be equal.
|
||||||
if (
|
if (
|
||||||
wordHash === currentFrameHash
|
wordHash === currentFrameHash
|
||||||
&& stringsAreEqual(text.substr(charIndex, word.length), word)
|
&& text.substr(charIndex, word.length) === word
|
||||||
) {
|
) {
|
||||||
return charIndex;
|
return charIndex;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user