java - Time complexity of String matching and Integer Matching -


how different string matching , integer matching in terms of time complexity?

i'm asking in relation rabin karp algorithm. why faster compute hash code every substring , check equality hash code of given search string naive method of checking if of substrings match given string?

typically, when designing algorithms, assume machine model such 2 integers (that fit machine word) can compared in constant time. since strings can longer single machine word, comparing 2 strings of length n require o(n) comparisons. therefore, it's faster compare 2 machine words compare 2 strings.

the rabin-karp algorithm efficient because uses comparisons of hash code minimize number of times expensive string comparisons required. specifically, rabin-karp compares substring against pattern string if substring has same hash code original pattern string, eliminates lot of unnecessary work.

hope helps!


Comments