Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions strings/knuth_morris_pratt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def knuth_morris_pratt(text: str, pattern: str) -> int:
... for s in ("kn", "h_m", "rr", "tt", "not there")
... )
True
>>> knuth_morris_pratt("abc", "")
0
>>> knuth_morris_pratt("", "")
0
"""

# 1) Construct the failure array
Expand Down Expand Up @@ -47,6 +51,11 @@ def get_failure_array(pattern: str) -> list[int]:
Calculates the new index we should go to if we fail a comparison
:param pattern:
:return:

>>> get_failure_array("abc")
[0, 0, 0]
>>> get_failure_array("aabaabaaa")
[0, 1, 0, 1, 2, 3, 4, 5, 2]
"""
failure = [0]
i = 0
Expand Down
Loading