Rabin-Karp Algorithm for Pattern Searching

Rabin-Karp algorithm also slides the pattern one by one. But unlike the Naive algorithm, Rabin Karp algorithm matches the hash value of the pattern with the hash value of current substring of text, and if hash values match then only it starts matching individual characters. Else It recomputes the hash value using Rolling Hash Function.

Algorithm:

Last updated