Definition · The window hashFix a base b and a modulus q. For a string w of length m the hash is the value of w read as an m-digit number in base b, taken modulo q: H(w) = (w[0] b^(m−1) + w[1] b^(m−2) + ... + w[m−1]) mod q, where each character contributes its code point.
Theorem · The roll is correctLet w = t[s..s+m−1] and w' = t[s+1..s+m]. Then H(w') = (b (H(w) − t[s] b^(m−1)) + t[s+m]) mod q, so each successive hash costs a constant number of arithmetic operations once b^(m−1) mod q is known.
Write H(w) without the reduction as S = sum over i of t[s+i] b^(m−1−i). Subtracting t[s] b^(m−1) removes the leading term, leaving sum over i ≥ 1 of t[s+i] b^(m−1−i).
Multiplying by b raises every remaining exponent by one, giving sum over i ≥ 1 of t[s+i] b^(m−i), which is the first m − 1 terms of w''s expansion. Adding t[s+m] b^0 completes it.
Reduction modulo q commutes with addition and multiplication, so performing it at each step gives the same residue as performing it at the end. The cost is one multiplication, one subtraction, one addition and one reduction, independent of m.
□
So the scan is n steps of constant arithmetic plus m character comparisons for each candidate. The panel counts the second part and prints it as characters compared verifying, because that is the part whose size depends on the text.
Example · Seven candidates, four matches, three collisionsThe lab opens on the rain in spain stays mainly in the plain and the pattern ain, at base 256 and modulus 41. The pattern hashes to 36. Of the 41 windows, 7 hash to 36. Four are the real occurrences, at offsets 5, 14, 25 and 40. The other three are at offsets 33, 36 and 38, and their characters are the space followed by th, then e space p, then pla. None of those is ain and all of them hash to 36.
The panel prints those three in a table with their hashes beside the pattern's, so that a reader can copy one out and check it. The worked example below does exactly that for the first of them, in five lines of arithmetic.
What the collisions cost, and what the cost does not depend on
Each of the 7 candidates costs m = 3 character comparisons to settle, whether it turns out to be a match or not, so the verification bill is 21 comparisons. Nine of those — three collisions at three characters each — produce nothing. The panel reports both numbers, as characters compared verifying and wasted on collisions.
Nothing in the algorithm notices the waste. A collision is indistinguishable from a match until the characters are compared, which is why the comparison is unconditional and why an implementation that trusted the hash would be wrong on this very text, at three offsets, silently.
Exact arithmetic, and why it is not a detail here
At base 256 a three-character window is a number up to 256^3, about 16.7 million, which fits a double comfortably. At twelve characters it does not, and a rounded intermediate would change which windows collide. The kit computes every hash in arbitrary-precision integers, so the three offsets above are the same three on every machine, and a reader who checks one by hand and disagrees has found a bug rather than a rounding difference.
The same choice is what makes the panel's independent recomputation meaningful. Each window's rolled hash is compared with the same window hashed from its characters, and the count of disagreements is printed; on this text it is 0. If the arithmetic were approximate that check would be comparing two approximations and could pass while both were wrong.
Where the cost actually goes
Rabin–Karp's attraction is that the scan is arithmetic rather than comparison, so the work per window does not depend on m. Its exposure is that the number of candidates does depend on the modulus, and the modulus is a choice. Three collisions on 41 windows is a 7 per cent false-alarm rate on this text at this modulus, and the sibling lesson “Choosing a Modulus, and What the Sweep Says” runs the same text at every prime under 200 to show what that rate does as the choice moves.