Boyer-Moore String Matching
Compare pattern characters right to left and shift by bad-character and good-suffix information after a mismatch.
Core idea
Compare the pattern from right to left. A mismatch uses knowledge of the mismatching text character, and in the full algorithm the matched suffix, to shift past alignments that cannot work.
Read the visualization
The pattern row slides beneath the text. Comparisons move left within one window, while a mismatch highlights the bad-character lookup and the resulting multi-position jump.
Align the pattern at this text window and begin comparing from its right edge.
Complexity and tradeoffs
Time: Often sublinear; worst O(nm). Space: O(m+alphabet). Large safe shifts make it excellent in practice, while preprocessing depends on the chosen heuristics.
Where it fits
Boyer-Moore is excellent for long patterns over sizable alphabets and often reads fewer text characters than the text length. Small alphabets or adversarial inputs reduce its advantage.