Rabin–Karp algorithm or Karp–Rabin algorithm is a
string-searchingalgorithm that useshashingto findan exact match of a pattern stringin a text. It uses arolling hashto quickly filter out positions of the text that cannot match the pattern, and then checks for a match at the remaining positions. Generalizations of the same idea can be used to find more than one match of a single pattern, or to find matches for more than one pattern.To find a
single matchof a single pattern, the expected time of the algorithm islinear in the combined length of the pattern and text, although itsworst-casetime complexity is theproduct of the two lengths. To find multiple matches, the expected time is linear in the input lengths, plus the combined length of all the matches, which could be greater than linear.
Pseudocode
function RabinKarp(string s[1..n], string pattern[1..m])
hpattern := hash(pattern[1..m]);
for i from 1 to n-m+1
hs := hash(s[i..i+m-1])
if hs = hpattern
if s[i..i+m-1] = pattern[1..m]
return i
return not found
# rolling hash
s[i+1..i+m] = s[i..i+m-1] - s[i] + s[i+m]
# Multiple pattern search
function RabinKarpSet(string s[1..n], set of string subs, m):
set hsubs := emptySet
foreach sub in subs
insert hash(sub[1..m]) into hsubs
hs := hash(s[1..m])
for i from 1 to n-m+1
if hs ∈ hsubs and s[i..i+m-1] ∈ subs
return i
hs := hash(s[i+1..i+m])
return not found

Comments
Join the discussion for this article at here . Our comments is using Github Issues. All of posted comments will display at this page instantly.