The Rabin-Karp Algorithm: String Matching
The Rabin-Karp Algorithm: String Matching
Algorithm
STRING MATCHING
Background
String matching
Naïve method
n ≡ size of input string
m ≡ size of pattern to be matched
O( (n-m+1)m )
Θ( n2 ) if m = floor( n/2 )
We can do better
String Matching Problem
3 1 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
15 mod 11 = 4 equal to 4 -> spurious hit
3 1 4 1 5 9 2 6 5 3 5
59 mod 11 = 4 equal to 4 -> spurious hit
3 1 4 1 5 9 2 6 5 3 5
92 mod 11 = 4 equal to 4 -> spurious hit
3 1 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
35 mod 11 = 2 not equal to 4