AlgoPlus//strings / rabin-karp
Read the theory

Rabin-Karp

Compare a rolling hash first; only verify characters when the hashes collide.

Shift
Comparisons
Matches
Text
Pattern
Legend
Comparing
Match
Mismatch
AI Tutor Workspace
In a nutshell
Rabin-Karp speeds up the search by comparing numbers instead of strings. It turns the pattern, and each equally-long window of the text, into a number called a hash. Equal strings always share a hash, so any window whose hash differs from the pattern's is skipped instantly without comparing a single character. Only when the hashes match does it verify character by character, since different strings can occasionally collide on the same hash. A rolling hash recomputes each new window's number from the previous one in one step, which keeps the average cost linear.
Ready
Press play to begin the cinematic walkthrough.
Turn each window into a number (a hash). Numbers that differ can't be equal strings — so you skip almost everything and only verify on a hash collision.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Rabin-Karp.