Skip to content

Commit eb24c60

Browse files
kmp added
1 parent 5a3fbe9 commit eb24c60

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ I am learning new algorithms and will be making notes for all of thoses here, al
55

66
String matching algorithms
77
* [Naive](./naive.py)
8-
* [KMP (Knuth Morris Pratt) Pattern Searching](./kmp.py)
8+
* [KMP (Knuth Morris Pratt)](./kmp.py)

kmp.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
KMP (Knuth Morris Pratt) Pattern Searching Algorithm is used for finding the indices of the presence of a given
3+
substring pattern in a string. It is an improvement on Naive.
4+
If you do not know the algorithm from beforehand, please watch this https://www.youtube.com/watch?v=V5-7GzOfADQ from 7:41
5+
Time Complexity: O(n) or O(n+m)
6+
"""
7+
8+
9+
def kmp(s: str, pat: str):
10+
lps = [0] * len(pat)
11+
i, l = 1, 0
12+
while i < len(pat): # create pi table
13+
if pat[i] == pat[l]:
14+
l += 1
15+
lps[i] = l
16+
i += 1
17+
else:
18+
if l != 0:
19+
l = lps[l - 1]
20+
else:
21+
i += 1
22+
i, j = 0, 0
23+
indices = []
24+
while i < len(s):
25+
if s[i] == pat[j]:
26+
i += 1
27+
j += 1
28+
else:
29+
if j == 0:
30+
i += 1
31+
else:
32+
j = lps[j-1]
33+
if j == len(pat):
34+
indices.append(i - len(pat))
35+
j = lps[j-1]
36+
return indices
37+
38+
39+
if __name__ == '__main__':
40+
print(kmp("AABAACAADAABAABA", "AABA"))
41+
print(kmp("AABCCAADDEE", "FAA"))
42+
print(kmp("AAAAAAAAAAAAAAAAAA", "AAAAA"))
43+
print(kmp("AAAAAAAAAAAAAAAAAB", "AAAAB"))
44+
print(kmp("ABCDCDDDCDE", "ADE"))

naive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Naive algorithm is used for finding the indices of the presence of a given substring pattern in a string
2+
Naive algorithm is used for finding the indices of the presence of a given substring pattern in a string.
33
Time Complexity O(m*(n-m+1)) or O(nm) where n and m are the lengths of string snd substrings respectively
44
"""
55

0 commit comments

Comments
 (0)