-
Notifications
You must be signed in to change notification settings - Fork 505
Expand file tree
/
Copy pathkmp_search.py
More file actions
253 lines (192 loc) · 6.06 KB
/
kmp_search.py
File metadata and controls
253 lines (192 loc) · 6.06 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""
Author: ADWAITA JADHAV
Created On: 4th October 2025
Knuth-Morris-Pratt (KMP) String Matching Algorithm
Time Complexity: O(n + m) where n is text length and m is pattern length
Space Complexity: O(m)
The KMP algorithm efficiently finds occurrences of a pattern within a text
by using a failure function to avoid unnecessary character comparisons.
"""
import inspect
def kmp_search(text, pattern):
"""
Find all occurrences of pattern in text using KMP algorithm
:param text: string to search in
:param pattern: string pattern to search for
:return: list of starting indices where pattern is found
"""
if not text or not pattern:
return []
if len(pattern) > len(text):
return []
# Build failure function (LPS array)
lps = build_lps_array(pattern)
matches = []
i = 0 # index for text
j = 0 # index for pattern
while i < len(text):
if text[i] == pattern[j]:
i += 1
j += 1
if j == len(pattern):
matches.append(i - j)
j = lps[j - 1]
elif i < len(text) and text[i] != pattern[j]:
if j != 0:
j = lps[j - 1]
else:
i += 1
return matches
def build_lps_array(pattern):
"""
Build the Longest Proper Prefix which is also Suffix (LPS) array
:param pattern: pattern string
:return: LPS array
"""
m = len(pattern)
lps = [0] * m
length = 0 # length of previous longest prefix suffix
i = 1
while i < m:
if pattern[i] == pattern[length]:
length += 1
lps[i] = length
i += 1
else:
if length != 0:
length = lps[length - 1]
else:
lps[i] = 0
i += 1
return lps
def kmp_search_first(text, pattern):
"""
Find the first occurrence of pattern in text using KMP algorithm
:param text: string to search in
:param pattern: string pattern to search for
:return: index of first occurrence or -1 if not found
"""
if not text or not pattern:
return -1
if len(pattern) > len(text):
return -1
lps = build_lps_array(pattern)
i = 0 # index for text
j = 0 # index for pattern
while i < len(text):
if text[i] == pattern[j]:
i += 1
j += 1
if j == len(pattern):
return i - j
elif i < len(text) and text[i] != pattern[j]:
if j != 0:
j = lps[j - 1]
else:
i += 1
return -1
def kmp_count_occurrences(text, pattern):
"""
Count the number of occurrences of pattern in text
:param text: string to search in
:param pattern: string pattern to search for
:return: number of occurrences
"""
return len(kmp_search(text, pattern))
def kmp_search_overlapping(text, pattern):
"""
Find all occurrences including overlapping ones
:param text: string to search in
:param pattern: string pattern to search for
:return: list of starting indices where pattern is found
"""
if not text or not pattern:
return []
if len(pattern) > len(text):
return []
lps = build_lps_array(pattern)
matches = []
i = 0 # index for text
j = 0 # index for pattern
while i < len(text):
if text[i] == pattern[j]:
i += 1
j += 1
if j == len(pattern):
matches.append(i - j)
# For overlapping matches, use LPS to find next possible match
j = lps[j - 1]
elif i < len(text) and text[i] != pattern[j]:
if j != 0:
j = lps[j - 1]
else:
i += 1
return matches
def print_lps_array(pattern):
"""
Print the LPS array for a given pattern
:param pattern: pattern string
:return: string representation of LPS array
"""
if not pattern:
return "Empty pattern"
lps = build_lps_array(pattern)
result = f"Pattern: {pattern}\n"
result += f"LPS: {lps}\n"
result += "Index: " + " ".join(str(i) for i in range(len(pattern)))
return result
def kmp_replace(text, pattern, replacement):
"""
Replace all occurrences of pattern with replacement string
:param text: original text
:param pattern: pattern to replace
:param replacement: replacement string
:return: text with replacements made
"""
if not text or not pattern:
return text
matches = kmp_search(text, pattern)
if not matches:
return text
# Replace from right to left to maintain indices
result = text
for match_index in reversed(matches):
result = result[:match_index] + replacement + result[match_index + len(pattern):]
return result
def validate_pattern(pattern):
"""
Validate if a pattern is suitable for KMP search
:param pattern: pattern to validate
:return: True if valid, False otherwise
"""
if not pattern:
return False
if not isinstance(pattern, str):
return False
return True
def compare_with_naive(text, pattern):
"""
Compare KMP results with naive string search
:param text: text to search in
:param pattern: pattern to search for
:return: tuple (kmp_matches, naive_matches, are_equal)
"""
kmp_matches = kmp_search(text, pattern)
# Naive search
naive_matches = []
for i in range(len(text) - len(pattern) + 1):
if text[i:i + len(pattern)] == pattern:
naive_matches.append(i)
return kmp_matches, naive_matches, kmp_matches == naive_matches
def time_complexities():
"""
Return information on time complexity
:return: string
"""
return "Best Case: O(n + m), Average Case: O(n + m), Worst Case: O(n + m)"
def get_code():
"""
Easily retrieve the source code of the kmp_search function
:return: source code
"""
return inspect.getsource(kmp_search)