| Learn | Algorithm - Searching - Rabin karp |
|---|
| Rabin Karp The Rabin–Karp algorithm or Karp–Rabin algorithm is a string searching algorithm that uses hashing to find any one of a set of pattern strings in a text.Its average and best case running time is O(n+m) in space O(p), but its worst-case time is O(nm) where n is the length of the text and m is the length of the pattern. Algorithm implementation in java for string matching While calculating hash value we are dividing it by a prime number in order to avoid collision.After dividing by prime number the chances of collision will be less, but still ther is a chance that the hash value can be same for two strings,so when we get a match we have to check it character by character to make sure that we got a proper match. t =(d*(t - text.charAt(i)*h) + text.charAt(i+m))%q; This is to recalculate the hash value for pattern,first by removing the left most character and then adding the new character from the text. |
Read Article
https://develop.horje.com/learn/1434/reference