HomeArticlesAlgorithms & AI

The Rabin-Karp Algorithm: Efficient String Search with Rolling Hashes

A powerful technique for finding patterns in texts that balances speed and simplicity.

mysimulator teamUpdated June 2026≈ 3 min read▶ Open the simulation

What is the Rabin-Karp Algorithm?

The Rabin-Karp algorithm is a string-searching algorithm that employs hashing techniques to find all occurrences of a pattern in a given text. It was developed by Michael O. Rabin and Richard M. Karp in 1987. The core idea behind the algorithm is to use a rolling hash function, which allows for quick updates as the search window slides across the text.

The key advantage of this approach is that it can preprocess the pattern once and then perform efficient sliding and hashing operations on the text, making it particularly useful for searching in large datasets or streams.

How Does Rolling Hashing Work?

Rolling hash functions are designed to efficiently update a hash value when the search window shifts by one character. This is achieved through modular arithmetic, which allows for quick updates without recalculating the entire hash from scratch.

For example, if we use a polynomial rolling hash function with a small modulus, the new hash can be computed as `new_hash = (old_hash - old_char * base^(window_size-1)) * base + new_char mod m`, where `base` is the radix of the alphabet and `m` is the modulus.

live demo · related simulation● LIVE

Why Does Rabin-Karp Matter?

The Rabin-Karp algorithm offers a significant improvement in efficiency over simpler string search methods, especially when dealing with large texts. Its ability to quickly eliminate non-matching windows reduces the overall number of character comparisons needed.

This makes it particularly valuable in applications such as bioinformatics, where searching for specific DNA sequences within vast genomic data sets is crucial.

Real-World Applications

The Rabin-Karp algorithm has found extensive use in various fields. In text editors and search engines, it helps in quickly locating keywords or patterns within large documents.

In bioinformatics, it is used to identify specific sequences of nucleotides in DNA or RNA, which can be crucial for genetic research.

Frequently asked questions

How does the Rabin-Karp algorithm handle collisions?

Collisions occur when two different substrings have the same hash value. To resolve this, the algorithm typically performs a character-by-character comparison of the matching hashes to ensure an exact match.

What is the time complexity of Rabin-Karp?

The average and best-case time complexity of the Rabin-Karp algorithm is O(n + m), where n is the length of the text and m is the length of the pattern. However, in the worst case, it can degrade to O(n * m).

Can the modulus be chosen arbitrarily?

The choice of modulus affects the performance and collision rate of the rolling hash function. A smaller modulus generally reduces the chance of collisions but may increase the computational complexity.

Is Rabin-Karp always faster than brute-force string search?

While Rabin-Karp is often faster, especially for longer texts and patterns, its performance can vary based on the specific implementation and input characteristics. Brute-force methods may be more efficient in certain cases.

Try it live

Everything above runs in your browser — open Rabin–Karp — Rolling Hash String Search and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.

▶ Open Rabin–Karp — Rolling Hash String Search simulation

What did you find?

Add reproduction steps (optional)