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
11 changes: 7 additions & 4 deletions strings/boyer_moore_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ def bad_character_heuristic(self) -> list[int]:
"""

positions = []
for i in range(self.textLen - self.patLen + 1):
i = 0
while i <= self.textLen - self.patLen:
mismatch_index = self.mismatch_in_text(i)
if mismatch_index == -1:
positions.append(i)
i += 1
else:
match_index = self.match_in_pattern(self.text[mismatch_index])
i = (
mismatch_index - match_index
) # shifting index lgtm [py/multiple-definition]
# shift pattern so the last occurrence of the mismatched
# character in the pattern aligns with the text, while
# always making at least one position of progress
i = max(i + 1, mismatch_index - match_index)
return positions


Expand Down