-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
Description
Article: Rabin-Karp for String Matching
I guess the code implements the rolling hash version of the algorithm, which is vital to the good performance versus just hashing every possible substring. https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
With a good hash function, the hash collision rate is low, so it would only take on average O(t) time to compute the rolling hashes, then O(s) time to check each match to see if it's valid. For a single match, this is O(t + s) = O(t). For m matches, I guess this is O(t + ms).
The worst case is if the hash function matches every time. Then the performance degrades to O(ts).