forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knuth_morris_pratt.py
34 lines (33 loc) · 923 Bytes
/
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
"""
Given two strings text and pattern,
return the list of start indexes in text that matches with the pattern
using knuth_morris_pratt algorithm.
If idx is in the list, text[idx : idx + M] matches with pattern.
Time complexity : O(N+M)
N and M is the length of text and pattern, respectively.
"""
def knuth_morris_pratt(text, pattern):
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