The Rabin-Karp algorithm is a string-matching algorithm that uses hashing to find an exact match of a pattern string in a text. Instead of checking character by character, it uses a rolling hash function to compare hash values of the pattern with hash values of substrings in the text. This makes it particularly efficient for multiple pattern matching.
The key features of Rabin-Karp are:
Uses rolling hash function for O(1) hash updates
Handles multiple pattern matching efficiently
Average case time complexity of O(n+m)
Particularly useful in plagiarism detection and similar string matching problems
The main idea is to calculate hash values for the pattern and all possible substrings of the text of the same length as the pattern, then compare these hash values instead of comparing the strings directly.
Implementation
Python
C++
Complexity
Time complexity: O(∣T∣+∣S∣) where ∣T∣ is the length of text and ∣S∣ is the length of pattern