-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp.py
More file actions
71 lines (57 loc) · 2.23 KB
/
Copy pathkmp.py
File metadata and controls
71 lines (57 loc) · 2.23 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
"""Knuth-Morris-Pratt (KMP) string matching algorithm."""
from typing import List
def kmp_search(text: str, pattern: str) -> List[int]:
"""Finds all 0-indexed starting positions of a pattern within a text string.
Complexity Analysis:
Time Complexity: O(n + m) where n is text length and m is pattern length.
Space Complexity: O(m) to store the partial match prefix table.
"""
# Handle absolute empty input parameters cleanly
if not pattern or not text:
return []
text_len, pattern_len = len(text), len(pattern)
# Precompute the Longest Prefix Suffix (LPS) layout array
lps = _compute_lps(pattern)
match_indices: List[int] = []
i = 0 # Text crawling tracker index
j = 0 # Pattern crawling tracker index
while i < text_len:
# Match confirmed, advance both tracking index pointers
if pattern[j] == text[i]:
i += 1
j += 1
# Entire pattern verified successfully
if j == pattern_len:
match_indices.append(i - j)
# Rollback pattern tracker back utilizing tracking arrays
j = lps[j - 1]
# Character mismatch occurred during active tracking
elif i < text_len and pattern[j] != text[i]:
if j != 0:
# Fallback pattern pointer safely to match last known character segment
j = lps[j - 1]
else:
# No tracking segment remains, safely step main index text forward
i += 1
return match_indices
def _compute_lps(pattern: str) -> List[int]:
"""Computes the Longest Prefix Suffix (LPS) array for the KMP pattern."""
m = len(pattern)
lps = [0] * m
length = 0 # Length of the previous longest prefix suffix
i = 1
while i < m:
# Segment matches, build out values and save indices
if pattern[i] == pattern[length]:
length += 1
lps[i] = length
i += 1
else:
if length != 0:
# Shift backward to find a matching prefix sub-window
length = lps[length - 1]
else:
# Basecase hit, zero out matching indexes
lps[i] = 0
i += 1
return lps