Rabin-Karp explained in plain terms means understanding the string search that compares fingerprints instead of letters, a rolling hash that slides across a text and only stops to look closely when the numbers match. The central question is why a search would bother hashing at all when comparing characters directly is so straightforward, and the answer is that comparing numbers is faster than comparing strings, so a search that checks a hash first can skip most of the work.
Rabin-Karp is a string-searching algorithm created by Richard Karp and Michael Rabin in 1987, published in the IBM Journal of Research and Development. It hashes the pattern once, then rolls a hash across the text one character at a time, which means each window is checked as a single number, and because only windows whose hash matches the pattern get a full comparison, the average search runs in time proportional to the text plus the pattern.
This article sets out five facts about Rabin-Karp, what it is, how it works, and where it runs in the real world.
Key takeaways
- Rabin-Karp searches by comparing hashes, not letters.
- Karp and Rabin published it in 1987.
- Its rolling hash updates in constant time.
- It averages linear time over text and pattern.
- It still powers plagiarism and duplicate detection.
What Rabin-Karp is
Rabin-Karp is a string-searching algorithm that turns pattern matching into arithmetic, and that arithmetic is what makes it unusual among search designs. The pattern is treated as a number in some base, the text is scanned window by window, and each window is reduced to a number the same way, which means a match of numbers is a candidate and a mismatch is a skip, and because numbers compare in one step, most windows are dismissed instantly.
Its signature feature is the rolling hash. Most hash functions must read a whole window again, but a rolling hash updates from the previous window by dropping the outgoing character and adding the incoming one, which means each step costs constant time no matter how long the pattern is, and because the update is a few arithmetic operations, the scan stays fast even on long texts.
The scheme was a reaction to the cost of naive search. Checking a pattern against every position character by character wastes work on positions that cannot match, so Karp and Rabin asked whether a fingerprint could filter them first, because a search that only looks closely at candidates is a search that finishes sooner, and that practicality is what made Rabin-Karp worth building.
It became one of the best-known string algorithms in the world, and it is named in every discussion of the field. The paper appeared in March 1987, the method was adopted for multi-pattern search and two-dimensional matching, and it is still the first example engineers meet when they learn that hashing can speed up search, which means the scheme survives as a standard even though faster single-pattern methods exist.
Our guide to hashing explains the general hash functions that Rabin-Karp builds on, and our guide to SimHash explains the fingerprint that finds near-duplicate documents instead of exact matches.
How Rabin-Karp works
Everything starts with the pattern hash, which sets the target. The algorithm treats the pattern as a base-d number and reduces it modulo a prime, which means the target is a single integer, and because the same base and prime are used for the text, every window hash is directly comparable.
Each step begins with the first window, which starts the scan. The hash of the first length-m window of the text is computed the same way, which means the scan opens with a number to compare, and because the computation uses Horner’s rule, it folds one character at a time without large intermediate values.
The roll is what makes the design work as a whole. To move one character right, the algorithm subtracts the outgoing character’s contribution, multiplies the remainder by the base and adds the incoming character, which means the next window hash comes from the last one in constant time, and because the window never needs rescanning, the whole text is processed in a single pass.
The verification is what turns the scheme into a correct search. A hash match only means the window might equal the pattern, so the algorithm compares the actual characters before reporting a hit, which means a collision, called a spurious hit, is caught and discarded, and because verification is rare, it costs almost nothing on average.
The prime is what turns the scheme into a careful piece of arithmetic. A large prime modulus keeps the numbers inside a machine word, which means the hash stays fast and the collision rate stays low, and because a small modulus would flood the search with spurious hits, the choice of prime is part of the design.
The result comes from the scan, read out as positions or nothing. The algorithm reports every window that passes both the hash check and the character check, which means the answer is exactly the true matches, no more and no fewer, and that verdict is exactly what a text search needs to deliver.

Comparing Rabin-Karp with other string searches
Rabin-Karp is one of several ways to find a pattern in a text, and each was built with a different emphasis. Knuth-Morris-Pratt is the classic built on prefix tables, Boyer-Moore is the skip-ahead design built for long patterns, and Rabin-Karp is the one built on hashing, while each approach trades something for something else.
The table below sets out the main contrasts between the leading approaches.
| Aspect | Rabin-Karp | Knuth-Morris-Pratt | Boyer-Moore |
|---|---|---|---|
| Preprocessing | Pattern hash | Prefix table | Skip tables |
| Average time | Linear in text | Linear in text | Sublinear often |
| Worst time | Quadratic | Linear | Quadratic |
| Multiple patterns | Yes, naturally | No, one at a time | No, one at a time |
| Best for | Many patterns | Single pattern | Long patterns |
The practical result is that Rabin-Karp is the right choice for many patterns, Knuth-Morris-Pratt suits a single pattern, and Boyer-Moore fits long patterns instead.
Where Rabin-Karp is used
Plagiarism checks use it to find copied passages. A checker hashes every window of a document and looks for windows shared with another, because a shared fingerprint means a shared passage, and that detection is the reason Rabin-Karp earns its keep in academic integrity.
Duplicate detection uses it to spot repeated content. Storage systems and search engines hash chunks of files to find copies, because a chunk whose fingerprint matches another chunk is almost certainly the same data, and that economy is the reason the design was built for large corpora.
File synchronisation uses it to split data into chunks. Tools in the rsync family use content-defined chunking based on rolling hashes, because a chunk boundary that follows the content survives small edits, and that stability is the reason Rabin-Karp appears in backup far beyond its search origins.
Bioinformatics uses it to match DNA sequences. Genomes are searched for short patterns called k-mers, because a rolling hash can scan millions of bases quickly, and that speed shapes how new sequence tools are configured.
Teaching uses it as the hashing search example. Courses introduce Rabin-Karp when they explain that a hash can stand in for a string, because it is the clearest case of arithmetic replacing comparison, and a student who understands Rabin-Karp understands why fingerprints matter.
Strengths and limits
The greatest strength of Rabin-Karp is its multi-pattern reach. A single scan can hunt for many patterns at once by checking one hash table, and that strength is real, measured in the checkers that rely on it and confirmed by the systems that keep using it.
It is also a strength that the method is simple. A polynomial hash and a rolling update is all an implementation needs, and that simplicity is worth more than cleverness in a field where a search that needs a manual is a search that will be implemented wrong.
The limits are real, though, and the biggest is the worst case. A text full of spurious hits forces a character comparison at every window, which means the running time can degrade to quadratic, and a system that needs guaranteed speed must choose a method with a linear worst case.
Rabin-Karp checklist
- Use Rabin-Karp for many patterns, never for a single long one.
- Remember Karp and Rabin published it in 1987.
- Note the hash filters windows before any comparison.
- Compare it with Knuth-Morris-Pratt to see hashing versus tables.
- Study the rolling update, which is what makes it fast.
- Check the modulus, since a small prime means many false hits.
- Keep verification in place, since collisions are possible.
- Read the 1987 paper to see how the method was set.
- Choose Rabin-Karp when fingerprints can do the filtering.
Frequently asked questions
What is Rabin-Karp in simple terms?
It is a string search that hashes the pattern and every window of the text, then only compares characters where the hashes match.
Who created Rabin-Karp?
Michael Rabin and Richard Karp, who published it in the IBM Journal of Research and Development in March 1987.
How is Rabin-Karp different from Knuth-Morris-Pratt?
Both scan in linear average time, but Rabin-Karp filters with a rolling hash and handles many patterns at once, while Knuth-Morris-Pratt uses a prefix table for one pattern.
Is Rabin-Karp still secure?
It was never meant for security, and its hash is easy to collide on purpose, so it should only be used for search and detection tasks.
Where is Rabin-Karp used?
In plagiarism checks, in duplicate detection, in file synchronisation, and in DNA sequence matching.
When should I use Rabin-Karp?
When you search for many patterns, when you chunk files by content, or when you study rolling hashes.
The bottom line
Rabin-Karp explained in one line is this: a fingerprint before a look, a 1987 search that let numbers do the filtering and taught the field that comparing hashes is faster than comparing strings.
It is proof that a rolling hash can outlive the era that built it, and the reason it still matters is that every fingerprint search designed since has been judged partly by the lesson Rabin-Karp taught.
This article is general information about how Rabin-Karp works, not advice about choosing a string-search method for a particular system or workload.







