Skip to content

Rabin-Karp

words

Level 2 of 3

Also called Karp-Rabin algorithm and Rolling hash search.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from rabin-karp's own run on the numbers shown.

choose one

question 1

Rabin-Karp looks for ate in abdicate. The fingerprints agree only at places 2 and 6. How many letter comparisons does it make?

the answer, and why

the answer is B

Place 2, bdi: b is not a, 1. Place 6, ate: a, t and e match, 3. No other place compares. That is 4.

if you chose another

  • That compares all 3 letters at both places, but place 2 stops at its first letter.
  • That counts only the match, not the chance agreement at place 2.
  • That compares all 3 letters at all 6 places, the most any search of 8 letters and 3 could make.

new numbers

question 2

Each text has 10 letters and each pattern 5. Which makes Rabin-Karp compare the fewest letters?

the answer, and why

the answer is B

aabaa is aaaaa plus 676: its middle letter, worth 26 times 26, is b (2), not a (1). 676 leaves 5, so aabaa leaves 5, never aaaaa's 0. No place agrees, and no letter is compared.

if you chose another

  • aaaaa and bbbbb both leave 0, so every place agrees by chance and compares 1 letter, 6 in all.
  • babab matches at places 2, 4 and 6, 5 letters each, and no other place agrees, so 15.
  • ccccc's number is 3 times aaaaa's, so it leaves 0 too: every place agrees by chance and compares 1 letter, 6 in all.

new numbers