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