← Course home

Advanced applications · Lesson 47 of 52

String search algorithms

Trace KMP fallback and rolling-hash candidate verification for pattern matching.

Full lessonAbout 10 min

You will learn to trace KMP fallback after mismatch and explain candidate verification in Rabin–Karp.

Recommended first: Arrays and Python sequence costs.

On this page 6 sections

The idea

kmp_search finds ababa inside ababcababa at index 5.

A naive search tries the pattern at every index of the text and compares characters until one differs. Knuth-Morris-Pratt avoids restarting the text index after a partial match. kmp_search of pattern ababa in text ababcababa returns 5. Index 0 matches four characters and then fails, because the text has c where the pattern needs a. The failure links say how long a prefix of the pattern is also a suffix of the part just matched. The search then resumes in the middle of the pattern instead of at its first character. rabin_karp_search on the same strings also returns 5. It compares a rolling hash of the pattern with the hash of each window, and it still checks the characters when the hashes agree. A hash hit is a candidate, not a proof.

A worked trace

StepStateWhat changed
Scanstate = 1At text index 0 the pattern state is 1.
Scanstate = 0At text index 4 the pattern state is 0.
Scanstate = 1At text index 5 the pattern state is 1.
Scanstate = 5At text index 9 the pattern state is 5.

Why it is correct

The failure link at a position is the longest proper prefix that is also a suffix of the pattern up to that position. Following it on a mismatch preserves the characters that are already known to match. That is why the text index does not move backward. Rabin-Karp's window hash can be updated in constant time by removing the character that leaves and adding the character that enters. Equal hashes can still be different strings, so the character check is part of correctness. Unequal hashes are a sure miss.

A common mistake

Equal hashes are not equal strings. Rabin-Karp still checks the characters.

What it costs

The failure table is linear in the pattern length. The search is linear in the text length. Extra memory is the failure table. The naive search is linear in the product of the two lengths. Rabin-Karp is linear in expectation and can degrade if every window collides. Use KMP when you want a worst-case linear bound. Use a rolling hash when you already have a modulus you trust and you verify candidates. The KMP search is Θ(n + m) time, and the failure table is Θ(m) extra memory.

Run it

python
from bitsandbytes.strings.algorithms import kmp_searchprint(kmp_search('ababcababa', 'ababa'))# prints: 5 

Practice and next step

Open Tries and text indexes next. A trie stores whole keys. A suffix array stores the sorted positions of one text.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.strings.algorithms

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

The first match is not at index 0

kmp_search and rabin_karp_search both find ababa inside ababcababa at index 5.

state
1

State

  1. 00comparing
  2. 11comparing

At text index 0 the pattern state is 1.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1KMP has matched a proper prefix and then the next character fails. What does the failure link do?
2Rabin-Karp reports equal hashes for a window and the pattern. What else is required?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.