diff --git a/strings/boyer_moore_search.py b/strings/boyer_moore_search.py index ad14a504f792..cd59cea4323c 100644 --- a/strings/boyer_moore_search.py +++ b/strings/boyer_moore_search.py @@ -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