Skip to content

Changed knuth_morris_pratt to be consistent with str.find() #9079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
14 changes: 7 additions & 7 deletions strings/knuth_morris_pratt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations


def kmp(pattern: str, text: str) -> bool:
def kmp(pattern: str, text: str) -> int:
"""
The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text
with complexity O(n + m)
Expand All @@ -24,7 +24,7 @@ def kmp(pattern: str, text: str) -> bool:
while i < len(text):
if pattern[j] == text[i]:
if j == (len(pattern) - 1):
return True
return i - j
j += 1

# if this is a prefix in our pattern
Expand All @@ -33,7 +33,7 @@ def kmp(pattern: str, text: str) -> bool:
j = failure[j - 1]
continue
i += 1
return False
return -1


def get_failure_array(pattern: str) -> list[int]:
Expand Down Expand Up @@ -61,22 +61,22 @@ def get_failure_array(pattern: str) -> list[int]:
pattern = "abc1abc12"
text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc"
text2 = "alskfjaldsk23adsfabcabc"
assert kmp(pattern, text1) and not kmp(pattern, text2)
print(kmp(pattern, text1), kmp(pattern, text2))

# Test 2)
pattern = "ABABX"
text = "ABABZABABYABABX"
assert kmp(pattern, text)
print(kmp(pattern, text))

# Test 3)
pattern = "AAAB"
text = "ABAAAAAB"
assert kmp(pattern, text)
print(kmp(pattern, text))

# Test 4)
pattern = "abcdabcy"
text = "abcxabcdabxabcdabcdabcy"
assert kmp(pattern, text)
print(kmp(pattern, text))

# Test 5)
pattern = "aabaabaaa"
Expand Down