Overview
Rabin-Karp uses a hash function to convert text patterns into numerical values. The hash function typically looks like: f(x)=m0+m1x+...+mn−1xn−1, where each character gets mapped to a value in the formula.
When searching, the algorithm calculates the hash value of the pattern and compares it with hash values of text substrings of the same length. This clever approach allows it to quickly skip sections that couldn't possibly match.
For example, in the text "AABAACAADAABAABA", the pattern "AABA" appears at positions 0, 9, and 12. Instead of checking every position character by character, Rabin-Karp uses hash values to identify potential matches.
🔍 Remember: The hash function is what makes this algorithm efficient—it allows you to quickly compare patterns without checking every single character!