|
The Rabin-Karp Algorithm for finding a pattern within a piece of text |
|
The Rabin-Karp Algorithm for finding a pattern within a piece of text with complexity O(nm), most efficient when it is used with multiple patterns as it is able to check if any of a set of patterns match a section of text in o(1) given the precomputed hashes.
This will be the simple version which only assumes one pattern is being searched for but it’s not hard to modify
Calculate pattern hash
length as the pattern calculating the hash of the text within the window compare it with the hash of the pattern. Only testing equality if the hashes match
>>> test_rabin_karp()
Success.