Skip to content

Commit bb57a9d

Browse files
author
deepshekhardas
committed
fix: make bad character shift effective in boyer_moore_search
The for-loop variable reassignment had no effect; use a while loop so the bad-character rule actually skips positions (O(n/m) not O(nm)). Fixes #14844
1 parent f5988cc commit bb57a9d

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

strings/boyer_moore_search.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ def bad_character_heuristic(self) -> list[int]:
8686
"""
8787

8888
positions = []
89-
for i in range(self.textLen - self.patLen + 1):
89+
i = 0
90+
while i <= self.textLen - self.patLen:
9091
mismatch_index = self.mismatch_in_text(i)
9192
if mismatch_index == -1:
9293
positions.append(i)
94+
i += 1
9395
else:
9496
match_index = self.match_in_pattern(self.text[mismatch_index])
95-
i = (
96-
mismatch_index - match_index
97-
) # shifting index lgtm [py/multiple-definition]
97+
i = max(i + 1, mismatch_index - match_index)
9898
return positions
9999

100100

0 commit comments

Comments
 (0)