Skip to content

Commit a4db14b

Browse files
Fix Boyer-Moore bad character shift to use while loop
The bad_character_heuristic method was reassigning the for-loop variable 'i' inside a for loop, which has no effect in Python. This caused the bad character shift to be ignored, making the algorithm behave as O(nm) brute-force instead of O(n/m). Fix: Replace for loop with while loop to allow the shift to take effect. Fixes #14844
1 parent e3b01ec commit a4db14b

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)