String Matching - Rabin Karp
Algorithm
- Read text and pattern.
- Compute hash of pattern.
- Compute hash of first window of text (window size = pattern length).
- Compare both hashes.
- If hashes are equal, compare characters one by one to confirm match.
- Slide window by 1 position using rolling hash.
- Repeat until all windows are checked.
Time Complexity
- Average: O(n + m)
- Worst: O(nm)
Space Complexity
- O(1)