3.8.5 · D1String Algorithms

Foundations — Boyer-Moore — bad character, good suffix heuristics

2,265 words10 min readBack to topic

Before you can understand the two clever "skip" rules (bad character and good suffix) in the parent note, you need a rock-solid grip on a handful of symbols and pictures. This page builds every single one from nothing. Read it top to bottom; each item leans on the one above it.


0. What is a string, really?

Figure — Boyer-Moore — bad character, good suffix heuristics

Look at the figure. The word ABCAB is drawn as five boxes. Under each box is its index: 0, 1, 2, 3, 4. So:

  • The first box is at index .
  • The last box is at index .

1. The two players: text and pattern

Because is the short one, whenever a match is even possible. And the pattern can only start at positions through — past that, the short strip would hang off the right edge of the desk.


2. Alignment and the shift

Figure — Boyer-Moore — bad character, good suffix heuristics

The figure shows the pattern strip CCTTTTGC placed with its left edge at on the text strip. Trace one vertical line: sits over , over , and in general:


3. Matching direction — comparing right to left

Figure — Boyer-Moore — bad character, good suffix heuristics
  • WHAT we do: put a finger on and on , check they match, then step both fingers one box left.
  • WHY we do it: if we get a mismatch after having matched, say, the last three boxes, we now know the text ends with those three pattern characters. That "known suffix" is exactly the fuel for the big-skip rules. Comparing left-to-right (like naive search) would only ever tell us about a matched prefix, which the skip rules cannot use.
  • WHAT IT LOOKS LIKE: in the figure the green ticks on the right show matched boxes; the red cross shows the first mismatch as we move left.

4. The mismatch index and the bad character

Figure — Boyer-Moore — bad character, good suffix heuristics

In the figure: matched suffix is coloured, the box at index is crossed out, and the text character under it is circled and labelled . Two facts to carry forward:

  1. The matched suffix has length . (Count the boxes: from up to .)
  2. The bad character is a text character — it may or may not appear anywhere inside .

5. Slice notation and "suffix / prefix"


6. Border — a chunk that is both a prefix AND a suffix


7. The last-occurrence function


8. The tools we borrow: , clamping, and "safe shift"

This "each rule is a safe lower bound, take the larger" idea is the logical glue of the whole algorithm — hold onto it when you read the parent's boxed final formula.


9. The alphabet and "sublinear"


Prerequisite map

String, index from 0

Text T length n, Pattern P length m

Slice notation prefix and suffix

Alignment shift s and T of s plus i

Right to left comparison

Mismatch index j and bad char c

Border prefix equals suffix

last of c function

Good suffix rule

Bad character rule

max and clamp to 1

Alphabet size and sublinear

Boyer-Moore search loop

Every foundation box on this page feeds one of the two skip rules, and both rules feed the final search loop — exactly the structure of the parent note.


Equipment checklist

Test yourself — each line is question ::: answer.

If a string has length , what is the index of its last box?
(indices run to ).
When the pattern is at shift , which text box sits under ?
.
In which direction does Boyer-Moore compare characters, and why?
Right to left, so a mismatch reveals a matched suffix that powers the big-skip rules.
What are and at a mismatch?
is the pattern index where the first (rightmost-first) mismatch occurs; is the bad text character under it.
What is a border of a string?
A proper substring that is both a prefix and a suffix.
What is when does not occur in ?
.
Why is the shift clamped with ?
To guarantee forward progress — a zero or negative shift would loop forever.
Why does Boyer-Moore take of the two rules' shifts?
Each rule is an independently safe jump; the larger of two safe jumps is still safe and faster.
What does "sublinear" mean here and when does it happen?
Reading fewer than text characters; happens with large alphabets where the bad character often isn't in , so jumps of size are common.

🇮🇳 Prefer Hinglish? See 3.8.05 Boyer-Moore — bad character, good suffix heuristics (Hinglish).