Rabin-Karp String Matching Algorithm efficiency

359 Views Asked by At

I know how the Rabin-Karp String Matching Algorithm works however not able to understand how its better than native method. In Rabin-Karp you find hash for every substring in a string and compare it with hash value of test string.And if it matches, you now compare the individual characters.However in native method, you just comapare the substring with the test string character by character. Isnt calculating the hash unnecessary and how is it faster than comparing individual characters?

1

There are 1 best solutions below

0
Petr Hála On

The hash is shorter than the string, possibly just one byte. If, for example, you make it by modulo 251 (highest prime number in 0-255 range), you make only a single byte comparison 99.6% of times if the compared strings are different.