Skip to main content
Back to the DSA atlas
Strings Algorithm Intermediate

Rabin-Karp

Search with rolling hashes so most windows can be rejected with arithmetic instead of full character comparisons.

stringrolling-hashpattern-matching

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

Window starting at 0

abracadabra

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

OperationTime
SearchExpected O(n+m)O(n + m) 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

ProblemDifficultyKey idea
Repeated DNA Sequences🟡 MediumRolling hash over fixed-length windows
Find Substring With Given Hash Value🔴 HardExplicit rolling-hash mechanics
Longest Duplicate Substring🔴 HardBinary 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.

More from Strings

Stay in the same family when you want parallel variations of the same mental model.

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.