Skip to content

Commit 22cec55

Browse files
Add files via upload
1 parent 13b5c8a commit 22cec55

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Original Code: https://github.com/MISabic/dsa-implementation/blob/main/string%20matching/KMP/KMP.py
2+
3+
def kmp_table(pattern):
4+
# Create a table to store the values of the longest proper prefix that is also a suffix of the substring for each position in the pattern.
5+
table = [0] * len(pattern)
6+
7+
# Initialize the left and right pointers to zero and one, respectively.
8+
left, right = 0, 1
9+
10+
# Iterate over the pattern from left to right
11+
while right < len(pattern):
12+
# If the character at the right pointer is equal to the character at the left pointer, increment both pointers and set the value of the table at the right pointer to the value of the left pointer.
13+
if pattern[right] == pattern[left]:
14+
left += 1
15+
table[right] = left
16+
right += 1
17+
18+
else:
19+
# If the characters are not equal, move the left pointer back to the position in the table corresponding to the previous longest proper prefix that is also a suffix, and continue checking for a match.
20+
if left != 0:
21+
left = table[left-1]
22+
23+
else:
24+
# If there is no previous longest proper prefix that is also a suffix, set the value of the tabe at the right pointer to zero and move pointer forward.
25+
table[right] = 0
26+
right += 1
27+
28+
29+
return table
30+
31+
32+
33+
def kmp_search(text, pattern):
34+
# Create a table to store the values of the longest proper prefix that is also a suffix of the substring for each position in the pattern.
35+
table = kmp_table(pattern)
36+
37+
# Initialize variables for the indicies of the text and pattern.
38+
i, j = 0, 0
39+
40+
# Iterate over the text while the index is less than the length of the text.
41+
while i < len(text):
42+
# If the characters at the current indicies match, increment both indicies.
43+
if text[i] == pattern[j]:
44+
i += 1
45+
j += 1
46+
47+
# If the value of j is equal to the length of the pattern, the pattern has been found in the text, so return the index where it starts.
48+
if j == len(pattern):
49+
return i - j
50+
51+
else:
52+
# If the characters do not match and j is not zero, move the j index to the value in the table corresponding to the previous longest proper prefix that is also a suffix, and continue checking for a match.
53+
if j != 0:
54+
j = table[j-1]
55+
else:
56+
# If there is no previous longest proper prefix that is also a suffix, move the i index forward.
57+
i += 1
58+
59+
60+
# If the pattern is not found, return -1
61+
return -1
62+
63+
64+
text = "Hello, world"
65+
pattern = "world"
66+
67+
pos = kmp_search(text, pattern);
68+
69+
70+
if pos == -1:
71+
print("Pattern not found")
72+
else:
73+
print("Pattern found at position", pos)
74+

0 commit comments

Comments
 (0)