Use '===' for double check string comparision in RabinKarp.

This commit is contained in:
Oleksii Trekhleb 2018-08-09 06:05:09 +03:00
parent d303d83673
commit e7d22b4883

View File

@ -1,29 +1,5 @@
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} word - Word that is being searched in text.
@ -52,10 +28,11 @@ export default function rabinKarp(text, word) {
prevFrame = currentFrame;
// 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 (
wordHash === currentFrameHash
&& stringsAreEqual(text.substr(charIndex, word.length), word)
&& text.substr(charIndex, word.length) === word
) {
return charIndex;
}