Rabin-Karp
In 1987, Richard M. Karp and Michael O. Rabin proposed a string matching algorithm that performs well in practice and generalizes string matching against a set of patterns. The Rabin-Karp algorithm takes O(m) time in its preprocessing stage and its worst-case running time is O((n - m + 1)m), similar to Boyer-Moore's.
To better introduce the Rabin-Karp algorithm, let's assume that our alphabet ∑ is composed only of decimal digits (∑ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}), so that we can view a string of k characters as a decimal number with length k. Therefore, string 12345 corresponds to number 12345. Given a pattern P[0...m] and a substring from text T[i...i + m], if we convert both those strings to their correspondent decimal number, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access