Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created September 15, 2015 07:05
Show Gist options
  • Select an option

  • Save rishi93/968bb1ba0ac66da21906 to your computer and use it in GitHub Desktop.

Select an option

Save rishi93/968bb1ba0ac66da21906 to your computer and use it in GitHub Desktop.
Rabin Karp Algorithm
from hashlib import md5
def rabinkarp(string,pattern):
string_len = len(string)
pattern_len = len(pattern)
pattern_bytes = pattern.encode()
pattern_hash = md5(pattern_bytes).hexdigest()
for i in range(0,string_len-pattern_len+1):
substring = string[i:i+pattern_len]
substring_bytes = substring.encode()
substring_hash = md5(substring_bytes).hexdigest()
if substring_hash == pattern_hash:
print "Pattern found at index ",i
rabinkarp("Hello world.This is world","world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment