forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmp_pattern_search.py
59 lines (51 loc) · 1.63 KB
/
kmp_pattern_search.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
# kmp_pattern_search.py
def kmp_pattern_search(text, pattern):
"""
Knuth-Morris-Pratt (KMP) algorithm for pattern searching.
This function finds all occurrences of 'pattern' in 'text'
using the KMP algorithm, which preprocesses the pattern for efficient searching.
Parameters:
text (str): The text in which to search for the pattern.
pattern (str): The pattern to search for.
Prints the starting index of each occurrence of the pattern.
"""
def compute_lps(pattern):
"""
Computes the Longest Prefix Suffix (LPS) array for the pattern.
Parameters:
pattern (str): The pattern to preprocess.
Returns:
list: The LPS array.
"""
lps = [0] * len(pattern)
length = 0
i = 1
while i < len(pattern):
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
lps = compute_lps(pattern)
i = j = 0 # Index for text and pattern
while i < len(text):
if text[i] == pattern[j]:
i += 1
j += 1
if j == len(pattern):
print(f"Pattern found at index {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
# Example usage
if __name__ == "__main__":
kmp_pattern_search("ababcabcab", "abc")