Rolling Hash Rabin–Karp · ALG 13.4

Characters are digits, P = 10, so H(x1…xJ) is simply the number x1…xJ mod N. Sliding the window: h := (P·h − σ[i]·PJ + σ[i+J]) mod N. If h = j (the needle's hash), compare the strings: a real occurrence, or a spurious hit that wastes J comparisons.

needle ι = j = H(ι) = window hash h =
real occurrence spurious hit
hash updates 0
occurrences 0
spurious hits 0
character comparisons 0
expected spurious ≈ (S−J+1)/N =
← ADS2 Arcade