Efficient
Boyer-Moore Algorithm
The standard for practical string search, skipping text using two heuristics.
Time Complexity
O(N/M) avg
Space Complexity
O(M + alphabet)
Interactive Visualization
Technical Guide
Boyer-Moore is the benchmark for practical string matching. It compares characters from right to left and uses two powerful heuristics—Bad Character and Good Suffix—to skip as much of the text as possible. It is most efficient when the alphabet is large and mismatches happen frequently near the end of the pattern.
Logical Phases
- 1Precompute bad character shifts.
- 2Align pattern and compare from right-to-left.
- 3On mismatch, skip text based on the heuristic.
- 4Achieve sub-linear performance in many cases.
Implementation
s += Math.max(1, j - badChar[text[s+j]]);Expert Pitfalls
Poor performance on very small alphabets (like binary) compared to KMP.