3.8.1 · D2String Algorithms

Visual walkthrough — Naive pattern matching — O(nm)

1,740 words8 min readBack to topic

Everything below is built from two strings. Let us name them once and never assume anything else.

We use (so ) and (so ) throughout.


Step 1 — Draw the two strings as a ruler over a tape

WHAT. Picture the text as a fixed tape, one box per character. Picture the pattern as a short transparent ruler you can slide left-to-right over that tape.

WHY. Before any comparing, we must see the one operation the whole algorithm is made of: placing the ruler at some position . If we cannot picture placement, we cannot picture the search.

PICTURE. The tape is fixed; the ruler starts at the far left, .

Figure — Naive pattern matching — O(nm)

Step 2 — Where is the ruler allowed to sit?

WHAT. Find the last legal position for the left edge .

WHY. If we slide the ruler too far right, its right edge points past the end of the tape — there is no character there to compare. So there is a hard wall. Finding it tells us exactly how many placements exist, which will directly control the running time later.

PICTURE. The last legal placement is when the ruler's right edge touches the tape's last box.

Figure — Naive pattern matching — O(nm)

Step 3 — At one placement, spell it out box by box

WHAT. Fix . Walk a pointer from upward, comparing against the text box underneath it, which is .

WHY. Placement alone proves nothing — we must check whether the letters actually agree. The pointer is our reading finger moving along the ruler. We compare left to right because we want to stop at the first disagreement (next step).

PICTURE. Green boxes = matched so far; the finger advances only while letters agree.

Figure — Naive pattern matching — O(nm)

Step 4 — A full match: the finger reaches the end

WHAT. If the finger walks all the way to without ever finding a mismatch, every letter agreed. Record as a valid shift.

WHY. is the signal "I compared all letters and none failed." That is exactly the definition of the pattern occurring at . Reaching the end is the only way to earn a match.

PICTURE. All three ruler boxes are green; is stamped as a hit.

Figure — Naive pattern matching — O(nm)

Step 5 — A mismatch: stop early and slide

WHAT. Now move to . Compare against . They differ immediately. We stop — no point checking — and slide the ruler one box right to .

WHY. Once a single pair disagrees, this placement can never become a full match. Continuing to compare would be wasted work. Stopping at the first mismatch is the early break; it never changes the answer, only saves effort. And we slide by exactly one box because naive matching throws away everything it just learned and restarts fresh (the thing KMP Algorithm later fixes).

PICTURE. Red box marks the first mismatch; a curved arrow shows the ruler jumping one step right.

Figure — Naive pattern matching — O(nm)

Step 6 — The whole slide across the tape

WHAT. Repeat placement + spelling for every and collect all valid shifts.

WHY. The naive method is correct by exhaustion: it tries every legal placement, so it cannot miss a real occurrence and cannot invent a fake one. Seeing all eight placements stacked makes the "try everything" nature visible.

PICTURE. Eight stacked rows, one per shift; green rows are the hits.

Figure — Naive pattern matching — O(nm)

Step 7 — The worst case: why the cost is

WHAT. Swap to the adversarial input (), (). Now at every placement the finger walks (three matches) and only fails on the last letter .

WHY. The early break was our savior — but here it never triggers early. Each of the placements pays the full comparisons before dying. Stacking those columns of near-full work is where the product literally appears as area on the page.

PICTURE. Each row is almost entirely green with a single red box at the far right — a wall of wasted work.

Figure — Naive pattern matching — O(nm)

The one-picture summary

Figure — Naive pattern matching — O(nm)

One diagram, the whole derivation: the ruler (, width ) slides over the tape (, length ); it may sit in spots; at each spot a finger spells left-to-right and stops on the first red; the total shaded area — placements times comparisons — is the cost.

Recall Feynman: retell the walkthrough with no symbols

You have a long sentence written on a paper strip and a short word written on a see-through ruler. You put the ruler at the very left. Then you read left to right: does the ruler's first letter match the paper letter under it? Second? If all of them match, you've found the word — mark that spot. The moment one letter is wrong, you stop reading (why keep going, it's already ruined?) and slide the ruler one notch to the right, starting your reading over from the ruler's first letter. You keep sliding until the ruler's right end would fall off the paper — that's your stopping wall. Most of the time on real sentences the very first letter is wrong, so you barely read anything and it's fast. But if the paper is "AAAAAAAA" and the word is "AAAB", every single time you read almost the whole word before the final B ruins it — so you do the maximum reading at every stop. Add up "number of stops" times "letters read per stop" and that rectangle of work is the famous .

Connections