From a4db14b49914b1e6ff180a2591ca6b0f0c7fa863 Mon Sep 17 00:00:00 2001 From: prince Date: Tue, 23 Jun 2026 22:58:28 +0530 Subject: [PATCH] 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 --- strings/boyer_moore_search.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/strings/boyer_moore_search.py b/strings/boyer_moore_search.py index ad14a504f792..cea4dbc2a7e0 100644 --- a/strings/boyer_moore_search.py +++ b/strings/boyer_moore_search.py @@ -86,15 +86,15 @@ 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] + i = max(i + 1, mismatch_index - match_index) return positions