On this page
Checking a pattern of m characters at every position of a text of n characters can take about n × m comparisons. Rabin–Karp turns each window into a number, compares numbers, and looks at characters only when the numbers agree.
What is the Rabin–Karp algorithm?
Rabin–Karp is a string-search algorithm by Richard M. Karp and Michael O. Rabin, published as "Efficient randomized pattern-matching algorithms" in the IBM Journal of Research and Development in March 1987 (the paper's DOI record). It hashes the pattern once, then hashes each window of the text of the same length; when a window's hash equals the pattern's hash, it does a full comparison at that position (Rabin–Karp algorithm, Wikipedia).
The hash is a polynomial: read the window's characters as digits in a base b and take the remainder mod a prime q. With decimal digits and b = 10, the window 314 is simply the number 314, and its hash mod 13 is 2.
How does the rolling hash update in O(1)?
Recomputing each window from scratch would save nothing. But neighbouring windows share m − 1 characters, so to slide one place you remove the leading digit's contribution, shift left, and add the new digit:
new hash = ((old hash − leading digit × b^(m−1)) × b + next digit) mod q
The constant b^(m−1) mod q is computed once. After that, every slide is one subtraction, two multiplications and one addition, however long the pattern is. If remainders feel rusty, our post on modular exponentiation by square and multiply works through them step by step.
Worked example: base 10, modulus 13
Search the text 31482653 (n = 8) for the pattern 265 (m = 3), with b = 10 and q = 13.
Pattern hash: 265 mod 13 = 5, because 13 × 20 = 260.
Constant: b^(m−1) = 100, and 100 mod 13 = 9.
First window: 314 mod 13 = 2, because 13 × 24 = 312.
Now roll. Each step drops the first digit, brings in the next one and reduces mod 13 (add 13 to a negative number until it lies between 0 and 12):
314 to 148: 2 − 3 × 9 = −25 ≡ 1, then 1 × 10 + 8 = 18 ≡ 5. A hit.
148 to 482: 5 − 1 × 9 = −4 ≡ 9, then 9 × 10 + 2 = 92 ≡ 1.
482 to 826: 1 − 4 × 9 = −35 ≡ 4, then 4 × 10 + 6 = 46 ≡ 7.
826 to 265: 7 − 8 × 9 = −65 ≡ 0, then 0 × 10 + 5 = 5. A hit.
265 to 653: 5 − 2 × 9 = −13 ≡ 0, then 0 × 10 + 3 = 3.
Check a few directly: 148 = 13 × 11 + 5, 482 = 13 × 37 + 1 and 653 = 13 × 50 + 3. Six windows, two hits, one real match.
What is a spurious hit, and how do you verify it?
A spurious hit, which Wikipedia calls a false positive, is a position whose hash equals the pattern's hash although its characters do not match the pattern (Rabin–Karp algorithm, Wikipedia). In the example, 148 hashes to 5 just like 265, because 265 − 148 = 117 = 9 × 13. Two numbers share a hash mod q exactly when their difference is a multiple of q.
So a hash match is only a candidate. Compare the window with the pattern character by character: 1 against 2 rejects 148 at the first digit, while 265 passes all three checks and is reported at position 5, counting from 1. Skip this check and you get a fast algorithm that sometimes lies. The same squeeze of many inputs into few hash values is behind hash table collisions and load factor and the birthday attack on hash functions.
How fast is Rabin–Karp, and when should you use it?
The expected running time of Rabin–Karp is linear in the combined length of the pattern and the text, but its worst case is the product of the two lengths (Rabin–Karp algorithm, Wikipedia). The slow case comes when nearly every window is a hit: report every match of aaa in a text of only a's and each window needs a full m-character check. To keep false positives rare, the same article says the hash function should be chosen at random from a family of hash functions that are unlikely to produce many of them.
For a single pattern, the article calls Rabin–Karp inferior to Knuth–Morris–Pratt and Boyer–Moore because of its slow worst case; Rabin–Karp earns its place when you search for many patterns at once. Store the hashes of k patterns of the same length m in a hash set, roll one hash across the text and look up each window: the expected time is O(n + km), against O((n + m)k) for k separate searches. The article names plagiarism detection as a practical use: searching a paper for sentences taken from source material.
How should you practise it?
A short checklist:
Fix b, q and m first, and compute b^(m−1) mod q once.
Reduce after every operation; bring negatives into 0 to q − 1 by adding q.
Check two rolled hashes by computing the window mod q directly.
Verify every hit character by character.
Use a tiny q, such as 7 or 13, to watch spurious hits appear.
Then redo the example with the pattern 482: you should get exactly one hit, and it should be real.
If modular arithmetic or hash functions are the weak link, the Fermi Concept Library covers every concept in mathematics and computing, each with its own lesson. For more practice, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



