We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 0e9e2d5 + 703114d commit 8b49743Copy full SHA for 8b49743
Strings/kmpstringmatching.py
@@ -0,0 +1,39 @@
1
+def KMPSearch(pat, txt):
2
+ M = len(pat)
3
+ N = len(txt)
4
+ lps = [0]*M
5
+ j = 0
6
+ computeLPSArray(pat, M, lps)
7
+
8
+ i = 0
9
+ while i < N:
10
+ if pat[j] == txt[i]:
11
+ i += 1
12
+ j += 1
13
14
+ if j == M:
15
+ print ("Found pattern at index " ,(i-j))
16
+ j = lps[j-1]
17
+ elif i < N and pat[j] != txt[i]:
18
+ if j != 0:
19
20
+ else:
21
22
+def computeLPSArray(pat, M, lps):
23
+ len = 0
24
+ lps[0] =0
25
+ i = 1
26
+ while i < M:
27
+ if pat[i]== pat[len]:
28
+ len += 1
29
+ lps[i] = len
30
31
32
+ if len != 0:
33
+ len = lps[len-1]
34
35
+ lps[i] = 0
36
37
+txt = "ABABDABACDABABCABAB"
38
+pat = "ABABCABAB"
39
+KMPSearch(pat, txt)
0 commit comments