Rabin-Karp
Search with rolling hashes so most windows can be rejected with arithmetic instead of full character comparisons.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
Window starting at 0
Pattern hash
114
Window hash
114
Rolling hashes skip most character comparisons
The hashes match, so Rabin-Karp verifies the characters and reports an occurrence.
Family
Strings
Prefix-aware structures and text-centric search strategies.
Builds on
Foundational
You can start here without another page first.
Unlocks
1 next topic
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
2
This topic appears in curated progressions so you can keep moving without guessing the next step.
Problem
You want to search for a pattern inside a text, but instead of building a prefix table like KMP, you want a method based on hashing.
Core idea
Treat a string window as a number modulo some base and modulus. As the window slides:
- remove the contribution of the outgoing character
- multiply / shift the remaining hash
- add the incoming character
That gives a rolling hash.
If the window hash does not equal the pattern hash, the window cannot match. Only when the hashes agree do you verify characters directly.
Why it matters
Rabin-Karp is valuable because it teaches string matching from the hashing angle instead of the prefix-function angle.
It is especially natural when:
- you search many windows of the same length
- you compare substrings quickly
- you combine hashing with binary search or suffix structures
Complexity
| Operation | Time |
|---|---|
| Search | Expected with low collision rate |
Worst case can degrade if collisions are adversarial, which is why practical implementations use large moduli or double hashing.
Key takeaways
- Rabin-Karp is about fast rejection by rolling hash
- Hash equality is a candidate signal, not a proof, unless the scheme is collision-free in context
- It complements KMP and Z rather than replacing them
- This is an important non-rare string tool because hashing shows up everywhere else too
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Repeated DNA Sequences | 🟡 Medium | Rolling hash over fixed-length windows |
| Find Substring With Given Hash Value | 🔴 Hard | Explicit rolling-hash mechanics |
| Longest Duplicate Substring | 🔴 Hard | Binary search plus substring hashing |
Relation to other topics
- KMP and Z-Algorithm solve pattern matching via prefix reuse instead of hashing
- Suffix Array often pairs with hashing for substring comparison tricks
- Hash Map ideas help explain why hashed string fingerprints can be useful at all
What this enables
Once the current idea feels natural, these are the most useful next jumps.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
KMP (Knuth-Morris-Pratt)
Search for a pattern in linear time by reusing information about its own prefixes instead of restarting after mismatches.
Z-Algorithm
Compute how much of the global prefix matches at every position, then reuse the active Z-box to stay linear.
Aho-Corasick
Match many patterns in one pass by augmenting a trie with failure links. This is multi-pattern search done at scale.
Manacher
Find the longest palindromic substring in linear time by reusing information from the rightmost known palindrome.
More from Strings
Stay in the same family when you want parallel variations of the same mental model.
KMP (Knuth-Morris-Pratt)
Search for a pattern in linear time by reusing information about its own prefixes instead of restarting after mismatches.
Trie (Prefix Tree)
A tree-like data structure for efficient string storage and prefix-based retrieval. Essential for autocomplete, spell checking, and word search problems.
Z-Algorithm
Compute how much of the global prefix matches at every position, then reuse the active Z-box to stay linear.
Aho-Corasick
Match many patterns in one pass by augmenting a trie with failure links. This is multi-pattern search done at scale.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
String matching
Move from prefix reuse and rolling hashes to suffix-based indexing and automata.
Substring toolkit
Compare rolling hashes, sorted suffixes, automata, and palindrome-specific structure.
From the blog
Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.