Matches
Comparisons
Alignments
  • Match
  • Mismatch
  • Known, not compared
  • Found

Algorithm Alignments Longest shift Comparisons per character

More information about string searching

String search visualizer

Finding a word in a document looks trivial, yet the obvious method can compare almost every character of the text with almost every character of the pattern. Cleverer algorithms learn from each comparison and jump ahead. Type a text and a pattern, or pick an example, and watch naive search, Knuth–Morris–Pratt, Boyer–Moore and Rabin–Karp slide the pattern along the text one alignment at a time: which characters they compare, how far they shift and why, and how many comparisons each one needs in the end. Type a text and a pattern, or pick an example, and choose an algorithm. The pattern sits under the text at the current shift: step through with Next step or Play. Green cells matched, red ones did not, and dashed blue cells are known to match without a new comparison (KMP); the small numbers give the order of the comparisons. The arrow shows the next shift and the line below says which rule gave it. The tables show what the algorithm works out from the pattern before it starts, and the chart counts the character comparisons of every algorithm on the same input. Click a row to switch to that algorithm.

How it works

  1. Type a text and a pattern, or choose an example such as ABABDABACDABABCABAB, a DNA strand or the worst case for naive search.
  2. Choose Naive, KMP, Boyer–Moore or Rabin–Karp and step through with Next step or Play: the colours show each comparison, the arrow the next shift, and the line below the rule that gave it.
  3. Read the tables the algorithm built from the pattern, and compare the character comparisons of all algorithms in the chart; click a row to switch.

Four ways to slide a pattern

Naive search tries every shift and compares from the left until the first mismatch, so a text of n characters and a pattern of m can take up to about n × m comparisons. Knuth–Morris–Pratt never moves backwards in the text: when a mismatch comes after some matching characters, its prefix function says how much of what matched can be reused. Boyer–Moore compares from the right end of the pattern and often jumps by the whole pattern length. Rabin–Karp compares a hash of each window with the hash of the pattern and only looks at the characters when the two agree.

Reading the shifts

Under the pattern an arrow shows the next shift, and the status line explains it with the numbers of this step: π[j] for KMP, λ(c) and the matched suffix for Boyer–Moore, the window hash for Rabin–Karp. The tables below light up the entry that was used. In the compare chart the same text and pattern are searched by all five algorithms, Horspool's simplified Boyer–Moore included, and every one must find the same matches.

Frequently asked questions

Which string search algorithm is the fastest?

It depends on the text and the pattern. Knuth–Morris–Pratt guarantees at most about 2n comparisons for a text of n characters, whatever the input. Boyer–Moore is usually the fastest in practice on normal text with a large alphabet, because it can skip a whole pattern length at a time and look at only a fraction of the characters, but its simple versions can be slow on repetitive input. Rabin–Karp shines when many patterns of the same length are searched at once.

What is the prefix function in KMP?

For every position j of the pattern, π[j] is the length of the longest proper prefix of P[0..j] that is also a suffix of it. After j matching characters and a mismatch, the pattern can move by j − π[j−1]: the first π[j−1] characters still match the text and need no new comparison. For ABABCABAB the table is 0 0 1 2 0 1 2 3 4.

What are the bad-character and good-suffix rules?

Boyer–Moore compares from the right. When a text character c does not match, the bad-character rule moves the pattern so that the last c in the pattern lies under it, or past it if c does not occur. The good-suffix rule moves the part that did match onto its next copy in the pattern. Both shifts are safe, so the algorithm takes the larger. Horspool's variant keeps only a bad-character table, read at the text character under the pattern's last cell.

What is a spurious hit in Rabin–Karp?

A window whose hash equals the hash of the pattern although the characters differ. The hash is a number modulo q, so different strings can share it; Rabin–Karp therefore checks the characters whenever the hashes agree. A small modulus gives many spurious hits, a large prime almost none: try the modulus 13 example.

What is the time complexity of naive string search?

In the worst case (n − m + 1) × m comparisons, O(nm), for a text of n characters and a pattern of m: on text like AAAA…AB with the pattern AAAB, nearly every shift matches three characters before failing. On ordinary text the first comparison usually fails, so it needs little more than n. Pick the example Worst case for naive search to watch it.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Go to Editor