You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Boyer-Moore bad character shift has no effect (for-loop variable reassignment) #14844
In strings/boyer_moore_search.py, the bad_character_heuristic() method attempts to skip positions using the bad character rule, but the shift has no effect because it reassigns the for-loop variable i:
defbad_character_heuristic(self) ->list[int]:
positions= []
foriinrange(self.textLen-self.patLen+1):
mismatch_index=self.mismatch_in_text(i)
ifmismatch_index==-1:
positions.append(i)
else:
match_index=self.match_in_pattern(self.text[mismatch_index])
i= (mismatch_index-match_index) # <-- THIS HAS NO EFFECTreturnpositions
In Python, reassigning the loop variable inside a for loop does not change the iteration. The next iteration always takes the next value from range(). This means:
The bad character shift is completely ignored
The algorithm checks every position sequentially
It behaves as brute-force O(nm), not Boyer-Moore O(n/m)
Results are correct, but performance is identical to naive search
This repository has 190k+ stars and is widely used as an educational reference. Students copying this implementation believe they have a Boyer-Moore algorithm with O(n/m) average performance, but actually have O(nm) brute-force search.
Activity
Fix Boyer-Moore bad character shift to use while loop