-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
knuth_morris_pratt.py
44 lines (40 loc) · 1.24 KB
/
knuth_morris_pratt.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 Sequence, List
def knuth_morris_pratt(text : Sequence, pattern : Sequence) -> List[int]:
"""
Given two strings text and pattern, return the list of start indexes in text that matches with the pattern
using knuth_morris_pratt algorithm.
Args:
text: Text to search
pattern: Pattern to search in the text
Returns:
List of indices of patterns found
Example:
>>> knuth_morris_pratt('hello there hero!', 'he')
[0, 7, 12]
If idx is in the list, text[idx : idx + M] matches with pattern.
Time complexity of the algorithm is O(N+M), with N and M the length of text and pattern, respectively.
"""
n = len(text)
m = len(pattern)
pi = [0 for i in range(m)]
i = 0
j = 0
# making pi table
for i in range(1, m):
while j and pattern[i] != pattern[j]:
j = pi[j - 1]
if pattern[i] == pattern[j]:
j += 1
pi[i] = j
# finding pattern
j = 0
ret = []
for i in range(n):
while j and text[i] != pattern[j]:
j = pi[j - 1]
if text[i] == pattern[j]:
j += 1
if j == m:
ret.append(i - m + 1)
j = pi[j - 1]
return ret