-
Notifications
You must be signed in to change notification settings - Fork 360
/
Rabin-Karp.py
58 lines (46 loc) · 1.91 KB
/
Rabin-Karp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def rabin_karp(text, pattern, base=256, prime=101):
"""
Rabin-Karp Algorithm for substring search.
Parameters:
text (str): The main text where the pattern is to be searched.
pattern (str): The pattern to search for.
base (int): The base used for hashing (default: 256).
prime (int): A prime number used to reduce hash values (default: 101).
Returns:
list: Starting indices of all occurrences of the pattern in the text.
"""
n, m = len(text), len(pattern)
if m > n:
return []
pattern_hash = 0 # Hash value for pattern
text_hash = 0 # Hash value for current window in text
h = 1 # The value of base^(m-1)
# Compute h = (base^(m-1)) % prime
for _ in range(m - 1):
h = (h * base) % prime
# Compute initial hash values for pattern and first window of text
for i in range(m):
pattern_hash = (base * pattern_hash + ord(pattern[i])) % prime
text_hash = (base * text_hash + ord(text[i])) % prime
# List to store starting indices of pattern matches
result = []
# Slide the pattern over text one character at a time
for i in range(n - m + 1):
# Check if the hash values match
if pattern_hash == text_hash:
# If hash matches, check characters one by one for a real match
if text[i:i + m] == pattern:
result.append(i)
# Compute hash for next window of text
if i < n - m:
text_hash = (base * (text_hash - ord(text[i]) * h) + ord(text[i + m])) % prime
# Make sure text_hash is positive
if text_hash < 0:
text_hash += prime
return result
# Example Usage
if __name__== "__main__":
text = "abracadabra"
pattern = "abra"
matches = rabin_karp(text, pattern)
print(f"Pattern '{pattern}' found at indices: {matches}")