Algorithms · Visualized

String: Boyer-Moore

String Matching

Find a pattern inside a text efficiently — from the naive slide to prefix-function smartness.

Boyer-Moore searches for a pattern by comparing it against the text from RIGHT to LEFT, and on a mismatch uses a precomputed bad-character table — the rightmost position of each pattern character — to shift the pattern forward by several positions in one jump. Time: O(n/m) with big alphabets average, O(n·m) worst; space: O(m + alphabet size) for the precomputed tables.

Big-O: Naive O(n·m); KMP & Z O(n+m); Rabin–Karp O(n+m) average with hashing.

Boyer-Moore String Search

String Matching

Boyer-Moore searches for a pattern by comparing it against the text from RIGHT to LEFT, and on a mismatch uses a precomputed bad-character table — the rightmost position of each pattern character — to shift the pattern forward by several positions in one jump. In practice this makes it sub-linear: it skips over large sections of text that can't possibly match.

How Boyer-Moore Works

  1. Precompute the bad-character table: for each character, its rightmost index in the pattern.
  2. Align the pattern with the text and compare from the pattern's rightmost character backward.
  3. On a mismatch at text position i (character c), shift the pattern so its rightmost occurrence of c lines up with i — or by 1 if c is not in the pattern (use the max with a safe 1).
  4. On a full match, record the position and shift the pattern forward by 1 to keep searching.
  5. The good-suffix rule can add an even larger shift based on the already-matched suffix.

Boyer-Moore Worked Example

Input: {text: HERE IS A SIMPLE EXAMPLE, pattern: EXAMPLE}

Align 'EXAMPLE' at the start of 'HERE IS A SIMPLE EXAMPLE'. Compare right-to-left: at text[6] = 'A'... the pattern shifts several positions per mismatch using the bad-character table, skipping most of 'HERE IS A SIMPLE'. It finally aligns over 'EXAMPLE' near the end and matches fully at index 17 — far fewer comparisons than a naive left-to-right scan.

String: Boyer-MooreO(n·m) worst, sub-linear avg
Scan right-to-left; the bad-character rule skips large sections on each mismatch.
t
H
E
R
E
I
S
A
S
I
M
P
L
E
E
X
A
M
P
L
E
01234567891011121314151617181920212223
p
E
X
A
M
P
L
E
0123456
Step 1 / 22
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Boyer-Moore: bad-character table — rightmost position of each character in "EXAMPLE": E→6, X→1, A→2, M→3, P→4, L→5.
Python · String Matching in Code
def kmp_search(text, pat):
    lps = [0] * len(pat)
    j = 0
    for i in range(1, len(pat)):
        while j > 0 and pat[i] != pat[j]:
            j = lps[j - 1]
        if pat[i] == pat[j]:
            j += 1
        lps[i] = j
    out = []
    j = 0
    for i, c in enumerate(text):
        while j > 0 and c != pat[j]:
            j = lps[j - 1]
        if c == pat[j]:
            j += 1
        if j == len(pat):
            out.append(i - j + 1)
            j = lps[j - 1]
    return out

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Boyer-Moore Time & Space Complexity

BestO(n/m) — sub-linear in practice
AverageO(n/m) with big alphabets
WorstO(n·m)
SpaceO(m + alphabet size) for the precomputed tables

When to Use Boyer-Moore

  • Text editors' find/replace and grep-like tools.
  • Virus signature scanning across many files.
  • DNA sequence pattern matching.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.