← capítulo

Rabin-Karp rolling hash

Compare NUMBERS, not strings. Hash each window; a rolling hash updates it in O(1) per slide.

The rolling hash

Watch the hash roll

Green = char that just entered · red = char that just left (toy modulus 101). 'abc' hashes to 22; window hits 22 → verify → match at 5.

Superpower: many patterns, one pass

Check each window's hash against a SET of k pattern hashes. 800 patterns: 200× faster than KMP×800.

Probabilistic, so verify

Takeaway

Randomization + hashing buy CAPABILITIES: fingerprint, dedup, batch-compare. The rolling hash outgrew the algorithm — rsync, backup chunking, plagiarism, git. Next: Boyer-Moore — scan right-to-left, skip forward, read fewer than n chars.