-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathkmp.py
44 lines (28 loc) · 1.15 KB
/
kmp.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
from typing import List
def compute_prefix_function(pattern: str) -> List[int]:
prefix_function = [0] * len(pattern)
matched = 0
for pos in range(1, len(pattern)):
while matched != 0 and pattern[pos] != pattern[matched]:
matched = prefix_function[matched - 1]
if pattern[pos] == pattern[matched]:
matched += 1
prefix_function[pos] = matched
return prefix_function
def kmp_match(string: str, pattern: str) -> int:
prefix_function = compute_prefix_function(pattern)
matched = 0
for pos in range(len(string)):
while matched != 0 and string[pos] != pattern[matched]:
matched = prefix_function[matched - 1]
if string[pos] == pattern[matched]:
matched += 1
if matched == len(pattern):
return pos - matched + 1
return -1
class TestKMP:
def test_compute_prefix_function(self) -> None:
assert compute_prefix_function("ababaca") == [0, 0, 1, 2, 3, 0, 1]
assert compute_prefix_function("aabab") == [0, 1, 0, 1, 0]
def test_kmp_match(self) -> None:
assert kmp_match("aaababaabaababaab", "aabab") == 1