To search a long text for a pattern, Rabin-Karp hashes a sliding window and compares hashes instead of characters. The trick is that when the window slides one step, you do not rehash from scratch: you subtract the contribution of the character leaving, shift the base, and add the character entering — an O(1) update instead of O(w). The window hash carries its state forward across the slide, so a whole-text scan drops from O(nw) to O(n). It powers substring search, rsync’s block matching, and plagiarism detection.
The demo hashes a 3-wide window, then rolls it one step by dropping the high term and adding the new char — the rolled hash equals a fresh recompute: live demo
“Every window must be hashed from its characters.” — only the first; each next window is one subtract, one shift, one add. cited
A window hash that walks the text one cheap update at a time — state carried across the slide, never rebuilt. incremental
On the canonical compiler, the rolled hash of window [2,5,9] (2086) equals a fresh recompute of the same window: