-
Notifications
You must be signed in to change notification settings - Fork 0
/
276.py
64 lines (50 loc) · 1.5 KB
/
276.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
64
"""
Problem:
Implement an efficient string matching algorithm.
That is, given a string of length N and a pattern of length k, write a program that
searches for the pattern in the string with less than O(N * k) worst-case time
complexity.
If the pattern is found, return the start index of its location. If not, return False.
"""
from typing import List, Union
def kmp_search(text: str, pattern: str) -> Union[int, bool]:
# modified kmp search to return the first match only
len_pattern = len(pattern)
len_text = len(text)
lps = compute_lps(pattern, len_pattern)
j = 0
i = 0
while i < len_text:
if pattern[j] == text[i]:
i += 1
j += 1
if j == len_pattern:
return i - j
elif i < len_text and pattern[j] != text[i]:
if j != 0:
j = lps[j - 1]
else:
i += 1
return False
def compute_lps(pattern: str, len_pattern: int) -> List[int]:
# computing the Longest Prefix which is also a Suffix
lps = [0 for _ in range(len_pattern)]
length = 0
i = 1
while i < (len_pattern):
if pattern[i] == pattern[length]:
length += 1
lps[i] = length
else:
lps[i] = length
i += 1
return lps
if __name__ == "__main__":
print(kmp_search("abcabcabcd", "abcd"))
print(kmp_search("abcabcabc", "abcd"))
"""
SPECS:
[n = length of text, m = length of pattern]
TIME COMPLEXITY: O(n + m)
SPACE COMPLEXITY: O(m)
"""