Skip to content

find a word in a text

Find where a short word, the pattern, stands in a longer text. Race four methods on the same text and pattern.

A step is one letter compared with another. For Rabin-Karp, working out a fingerprint, one number for a stretch of letters, is a step too.

In the race below each method has a lane. At each tick of the clock, every lane still running makes one more step. Fewest steps wins.

All four race on kookaburra, looking for burra. Burra's first letter can sit under any of the text's first 6 letters: at place 6 its last letter sits under the text's 10th. These are its 6 places, and it matches at place 6. A search finds every place the pattern matches, so it carries on after a match. Below, each method's steps on kookaburra.

Choose "a comparison" under "one tick is", and Rabin-Karp's fingerprints are not counted: on kookaburra it then wins with 5 comparisons, against Horspool's 7.

Brute force tries every place from the left. At each, it compares letters from the left until two differ or the whole pattern matches.

Burra under kookaburra at each of its 6 places: yellow the same, red different, plain not compared. At places 1 to 5 its b differs from the letter over it, 1 step each. At place 6 all 5 letters are the same. 10 steps.

Boyer-Moore-Horspool, Horspool for short, compares from the pattern's right end. Then it reads the text letter over that end, and jumps the pattern on until a copy of that letter, before the pattern's end, sits under it. With no such copy, it jumps right past. Its jumps come from a list made from the pattern first, which compares no letters, so costs no steps.

At place 1 the a at burra's end is the same, and k differs from r: 2 steps. Burra has no a before its end, so it jumps 5 places, right past the a, to place 6. There all 5 letters are the same. 7 steps.

Rabin-Karp works out a fingerprint for the pattern, and one for the stretch of text at each place. Each letter gets a number, a 1 up to z 26, multiplied by 26 once for each letter after it. The numbers are added, and the fingerprint is the remainder after dividing by 11. It compares letters only where the two fingerprints agree.

placefingerprintletters comparedsteps
burra itself601
1201
21001
3201
4001
5701
6656
all512
Burra is 2×264+21×263+18×262+18×26+12 \times 26^4 + 21 \times 26^3 + 18 \times 26^2 + 18 \times 26 + 1: b is 2, with 4 letters after it. That is 1,295,685, and 11 times 117,789 is 1,295,679, so 6 is left over. Only place 6's stretch, burra again, also leaves 6, so only there does it compare letters. 7 fingerprints and 5 comparisons: 12 steps.

Knuth-Morris-Pratt first compares the pattern with itself, to build a table. For each letter, the table holds how many of the pattern's first letters still line up after the letter just past it differs: for abab, 0, 0, 1, 2. So it slides the pattern on and never moves back in the text.

stageletters comparedsteps
its tableu, r, r and a against b, each different4
places 1 to 5k, o, o, k and a against b5
place 6b, u, r, r, a: all the same5
all14
No letter after burra's b is a b, so after a letter differs no start of burra still lines up: its table holds 0 for each letter. Then it compares each text letter once. 14 steps, the table's 4 included.

or make one

race these, up to four

one tick is

Without scripts the race cannot run, so each lane shows where its racer ends.

Tick 14: no lane has made more than 14 steps. Brute-force search, boyer-moore-horspool, rabin-karp and knuth-morris-pratt have finished.

brute-force search

done in 10 steps
kookaburraburra

boyer-moore-horspool

done in 7 steps
kookaburraburra

rabin-karp

done in 12 steps
kookaburraburra

knuth-morris-pratt

done in 14 steps
kookaburraburra

what the marks mean

  • letters compared and the same, or letters being read
  • letters compared and different
  • a letter of a match found
  • the letter a jump is worked out from
  • a letter no match can start at
  • any other letter
  • how much of its race a lane has run

which suits this text and pattern

On this text of 10 letters, boyer-moore-horspool made the fewest steps: 7, against 10 for brute-force search, 12 for rabin-karp and 14 for knuth-morris-pratt.

each racer's own page

The same racers on longer texts in one shape (a's, and a's), the pattern half the text: steps up, how many letters across, both scales in powers of ten. Each point is counted on that text.
102104106101102103104105106107101102103104105106101102103104105106107
  • brute-force search
  • boyer-moore-horspool
  • rabin-karp
  • knuth-morris-pratt

the starting racers, then on a longer text

Horspool wins on kookaburra, and Knuth-Morris-Pratt comes last. A text of a's and a pattern of a's turn that over: Knuth-Morris-Pratt wins, as the shapes below show, and a longer text widens the gap. Take 1,000 a's and a pattern of 500 a's. The pattern can stand at 501 places, 1,000 less 500, plus 1, and matches at every one. Brute force compares all 500 letters at each: 500 at each of 501 places, 250,500 steps. So does Horspool: the pattern has an a just before its end, so it jumps only 1 place each time. Rabin-Karp compares the same, plus 502 fingerprints, one at each place and the pattern's own: 251,002. Knuth-Morris-Pratt builds its table in 499 steps, one for each a after the first, then compares each text letter once: 499 + 1,000 = 1,499.

First, their steps on the starting text. Then on 1,000 a's and a pattern of 500 a's. The fewest in each row is in bold.

brute-force searchboyer-moore-horspoolrabin-karpknuth-morris-pratt
the starting textkookaburra, burra1071214
a text of 1,000 letters250,500250,500251,0021,499

the starting racers on a text of 10 letters in each shape

Each shape below is a kind of text and pattern the race can make. Horspool makes the fewest steps on three of the four. On aaaaaaaaaa and aaaaa every place matches: Horspool makes 30 steps, as the picture below shows, and Knuth-Morris-Pratt 14. On aaaaaaaaaa and bbbbb, every stretch of 5 letters in the text is aaaaa. The stretch aaaaa is 475,255, which is 11 times 43,205, and bbbbb is twice that, 950,510, so both leave 0 after dividing by 11. So every fingerprint agrees by chance, and Rabin-Karp compares a letter at all 6 places, beside its 7 fingerprints: 13 steps. Use Horspool on everyday text, where it jumps far. Avoid it where the pattern ends in a run of one letter that the text repeats: it then compares that run again at every place and jumps 1. Against ten a's, aaaaa and baaaa each cost it 30 steps. On aaaaa, which matches at every place, Knuth-Morris-Pratt makes 14, the fewest. On baaaa brute force makes 6, as its first letter, b, differs at once, and Knuth-Morris-Pratt 10: 4 for its table, then its b against the text's first 6 a's.

methodletters comparedalsosteps
brute force5 at each of 6 places30
Horspool5 at each of 6 places30
Rabin-Karp5 at each of 6 places7 fingerprints37
Knuth-Morris-Pratteach a once: 10its table: 414
Ten a's and aaaaa match at every place. Three methods check the whole pattern again at each; Knuth-Morris-Pratt never does.
Horspool on aaaaaaaaaa and aaaaa. The a just before the pattern's end lines up after a jump of 1, so it stands at all 6 places and compares all 5 letters at each.
Horspool on aaaaaaaaaa and baaaa. The a before the end lines up after a jump of 1, so it stands at all 6 places. At each, from the end, four a's are the same, then the b differs: 30 steps.

Their steps on the text and pattern the race makes in each shape.

brute-force searchboyer-moore-horspoolrabin-karpknuth-morris-pratt
a's, and a's ending in baaaaaaaaaa, aaaab306722
a's, and a'saaaaaaaaaa, aaaaa30303714
a's, and b'saaaaaaaaaa, bbbbb621310
random letters, the pattern in thembabccabcbc, bccab1161212