AlgoScope

Rabin-Karp

Compare a rolling hash of each window with the pattern hash and read characters only on a hash hit.

String algorithms, algorithm, intermediate
Match at 0.txtpatabracadabraabra

String Matching: Rabin-Karp rolling hash

Decision, step 2 of 10

Shift 0, window "abra" hashes to 57. Equal to the pattern hash 57, and the characters agree: a match at index 0.

How string matching works

Cost

Best
O(n + m)
Average
O(n + m)
Worst
O(n * m)
Space
O(1)

Worst case from hash collisions. Expected linear.

Taught by the same lesson

String Matching covers these too, in the same run.