Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 28eafd7

Browse files
Knuth–Morris–Pratt algorithm
1 parent 14f0304 commit 28eafd7

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ Data Structures and Algorithms Patterns implemented in Python.
2828
- [x] [Shell Sort](Sorting-Algo/shellsort.py)
2929
- [x] [Selection Sort](Sorting-Algo/selectionsort.py)
3030
- [x] [Bucket Sort](Sorting-Algo/bucketsort.py)
31+
- [x] [Strings](Strings)
32+
- [x] [KMP (Knuth Morris Pratt) Pattern Searching](Strings/KMP.py)
3133

‎Strings/KMP.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'''
2+
The Knuth-Morris-Pratt (KMP)Algorithm
3+
4+
For a given string ‘S’, string matching algorithm determines whether a pattern ‘p’ occurs in the given string ‘S’.
5+
6+
Input :
7+
8+
String = "Welcome to CodeKMPPattern"
9+
10+
Pattern = "Code"
11+
Output :Pattern found at index 11.
12+
13+
Time Complexity - O(n)
14+
'''
15+
def KMPSearch(pat, txt):
16+
M = len(pat)
17+
N = len(txt)
18+
19+
# create lps[] that will hold the longest prefix suffix
20+
# values for pattern
21+
lps = [0]*M
22+
j = 0 # index for pat[]
23+
24+
# Preprocess the pattern (calculate lps[] array)
25+
compute(pat, M, lps)
26+
27+
i = 0 # index for txt[]
28+
while i < N:
29+
if pat[j] == txt[i]:
30+
i += 1
31+
j += 1
32+
33+
if j == M:
34+
print ("Found pattern at index", str(i-j))
35+
j = lps[j-1]
36+
37+
# mismatch after j matches
38+
elif i < N and pat[j] != txt[i]:
39+
# Do not match lps[0..lps[j-1]] characters,
40+
# they will match anyway
41+
if j != 0:
42+
j = lps[j-1]
43+
else:
44+
i += 1
45+
46+
def compute(pat, M, lps):
47+
len = 0 # length of the previous longest prefix suffix
48+
49+
lps[0] # lps[0] is always 0
50+
i = 1
51+
52+
# the loop calculates lps[i] for i = 1 to M-1
53+
while i < M:
54+
if pat[i]== pat[len]:
55+
len += 1
56+
lps[i] = len
57+
i += 1
58+
else:
59+
# This is tricky. Consider the example.
60+
# AAACAAAA and i = 7. The idea is similar
61+
# to search step.
62+
if len != 0:
63+
len = lps[len-1]
64+
65+
# Also, note that we do not increment i here
66+
else:
67+
lps[i] = 0
68+
i += 1
69+
70+
txt = "ABABDABACDABABCABAB"
71+
pat = "ABABCABAB"
72+
KMPSearch(pat, txt)
73+
#Found pattern at index 10

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /