Skip to content
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

Playback Speed
Execution State
STEP 1 / 14
Text
F
0
I
1
N
2
D
3
I
4
N
5
A
6
H
7
A
8
Y
9
S
10
T
11
A
12
C
13
K
14
N
15
E
16
E
17
D
18
L
19
E
20
Pattern (Right-to-Left)
N
0
E
1
E
2
D
3
L
4
E
5
Phasecompare
Shift0
Process Description

Aligning pattern at index 0. Comparing from right-to-left: pattern[5] ('E') vs text[5] ('N')

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

  • 1
    Precompute bad character shifts.
  • 2
    Align pattern and compare from right-to-left.
  • 3
    On mismatch, skip text based on the heuristic.
  • 4
    Achieve 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.