-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathKMP_StringMatching.py
63 lines (49 loc) · 1.5 KB
/
KMP_StringMatching.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
59
60
61
62
63
# To find indexes of String "Pattern" in a given String "Text" using KMP Algorithm
# function that returns list of indexes where the patters matches
def KMP_Search(pattern, text):
n = len(text)
m = len(pattern)
# pre-compute prefix array of the pattern
prefix_arr = get_prefix_arr(pattern, m)
# stores start point of pattern match in text
start_points = []
i = 0
j = 0
# while the whole text has not been searched
while i != n:
# if the character in text matches the pattern character
if text[i] == pattern[j]:
i += 1
j += 1
# else find the previous index from where the matching can resume
else:
j = prefix_arr[j-1]
# if pattern length has been reached that means a pattern has been found
if j == m:
start_points.append(i-j)
j = prefix_arr[j-1]
elif j == 0:
i += 1
# return the starting position of pattern in text
return start_points
# pre-computes the prefix array for KMP search
def get_prefix_arr(pattern, m):
prefix_arr = [0] * m
j = 0
i = 1
while i != m:
if pattern[i] == pattern[j]:
j += 1
prefix_arr[i] = j
i += 1
elif j != 0:
j = prefix_arr[j-1]
else:
prefix_arr[i] = 0
i += 1
return prefix_arr
txt = "ABABDABACDABABCABABCABAB"
pat = "ABABCABAB"
start_indexes = KMP_Search(pat, txt)
for i in start_indexes:
print(i)