Using the Rabin-Karp method for string matching, how many character-character matches does the method use? Where:
source = abcabcabcacd
pattern = bcac
Using the Rabin-Karp method for string matching, how many character-character matches does the method use? Where:
source = abcabcabcacd
pattern = bcac
Copyright © 2021 Jogjafile Inc.
Let's start from the algorithm itself. How does Rabin-Karp methods work. I will use C# code to illustration. Having
we compute hash for the
target
:and then efficiently compute hashes for all substring of
target
length (here of substrings of length4
):"abca"
,"bcab"
, ..."cacd"
. IftargetHash
equals to substring's hash we compare this substring andtarget
corresponding letters. For instance:if
targetHash = 888
and for substrings we have, say,Here we have 3 episodes of character-character matching. Now we are ready to answer your question: the number of character-character matching depends on hash function.
Worst case: we have nothing but hash collisions:
All 9 substrings should be character-character checked.
Best case: we have no hash collisions; say, we can implement a typical Rabin hash function as
And we should check the only 1 actual matching:
Outcome:
Final answer: Depending on hash function, we have to check from
1
up to9
times.