Rabin-Karp Algorithm
# Rabin-Karp Algorithm
# General Idea
- Convert the pattern (length m) to a number p
- Convert the first m-characters (the first text window) to a number t with a hash function
- If p and t are equal, there is possibility of pattern: verify against the actual pattern
- Rolling hash function: shift the text window one character to the right and convert the new string
- Repeat until pattern is found or exit
# Rolling hash function
The new hash value can be calculated in $\theta(m)$ time.
# Improving the hash function
# Pseudocode
# Complexity
# Examples
Rabin-Karp can support wildcard matches by simply considering the position of the wildcard to be a value of 0: