3.8.2 · D1String Algorithms

Foundations — KMP algorithm — failure function, O(n+m) — full derivation

2,527 words11 min readBack to topic

Two names we will use from the very first line, so let us fix them now:

Before you can follow the parent derivation, you must own — cold — every piece of notation it throws at you. This page introduces each one from nothing: plain words first, then the picture, then why the topic can't live without it. Read top to bottom; each idea leans on the one above.


1. Strings, characters, and indexing

We need a way to point at one specific bead. That is what indexing does.

Figure — KMP algorithm — failure function, O(n+m) — full derivation

Figure 1 (above) draws as seven boxed beads on a wire, with a small grey number under each box showing its index . An orange arrow points at the leftmost box to remind you it is (the first bead), and a plum arrow tags the middle box as . The picture matters because every later idea is really "point at these boxes and slice between them" — you must first see that positions are numbered from zero.

Why the topic needs it. KMP is a story about two moving fingers — one on the pattern, one on the text. To say "the finger is here" precisely, we must be able to name each position. is that naming.


2. Substrings, prefixes, and suffixes

Now we slice pieces out of a string.

Two special substrings drive the whole algorithm: the front chunk and the back chunk.

Figure — KMP algorithm — failure function, O(n+m) — full derivation

Figure 2 (above) shows the string drawn twice. On the top copy the first three boxes are shaded and a teal bracket labels them "proper PREFIX 'aba'" — the front slice . On the bottom copy the last three boxes are shaded with a plum bracket labelled "proper SUFFIX 'aba'" — the back slice . The orange caption between them notes that the very same three letters appear at both ends. The figure matters because "front slice" versus "back slice" is abstract in words but obvious once you literally see the two shaded regions sitting at opposite ends of the same beads.

Why the topic needs it. The failure function asks a question purely about prefixes and suffixes: "how much does the front of my pattern also appear at the back of what I've matched?" Without these two words the definition of cannot even be stated. The word proper is not decoration — the parent note's first steel-man mistake shows that dropping it causes an infinite loop.

Recall Quick check: list prefixes and suffixes

For : proper prefixes are , ; proper suffixes are , . The common one is .


3. A border (prefix that is also a suffix)

This is the heart concept the whole cheat-sheet is built from.

Figure — KMP algorithm — failure function, O(n+m) — full derivation

Figure 3 (above) overlays two frames on the single string : a solid teal box around the first three beads (the front ) and a dashed plum box around the last three beads (the back ). The two frames physically overlap in the middle, which is the visual "aha" — the same three letters are being reused at both ends. The orange caption states the payoff, . Look at how the boxes share the centre bead: that shared overlap is exactly why sliding the pattern to a border costs nothing to recheck.

Why the topic needs it. is literally "the length of the longest border of ." Everything else — the recurrence, the fallback chain, the linear time — is machinery for computing and using borders. See Borders and Periods of Strings for the deeper theory of why borders come in neat nested chains.


4. The failure function and the notation

So is not one number; it is a whole row of numbers, computed once, then consulted. Here is the complete table for — every index through , none skipped, so you can see what lives in each slot:

longest border
0 a (empty) 0
1 ab (empty) 0
2 aba a 1
3 abab ab 2
4 ababa aba 3
5 ababac (empty) 0
6 ababaca a 1

Read it as: "at slot , look at the front-piece , find its longest border, and record that border's length." A 0 never means "missing" — it means the only border is the empty string.

Why the topic needs it. is the cheat-sheet itself. Building it is the "preprocessing" half of KMP; reading it during the scan is the "matching" half. When you see in the parent note, it means "look up slot of the cheat-sheet to find the next border length to try."


5. The two fingers and

Figure — KMP algorithm — failure function, O(n+m) — full derivation

Figure 4 (above) stacks the text (teal boxes, top) over the pattern (plum boxes, bottom). A dotted orange line drops from a text box down to a pattern box to show the two fingers meeting at the current comparison; the top of the line is labelled "text finger " and an arrow into the pattern is labelled "pattern finger (chars matched so far)". The bold caption states the golden rule: only moves right, while rises slowly and falls by borders. The picture matters because the whole speed argument is a claim about how these two fingers are allowed to move, and it is far easier to trust once you have watched them line up.

Why the topic needs it. The entire O(n+m) magic is a statement about these fingers: never moves backward, and can only rise slowly but fall by borders. Keeping the two fingers straight is what separates "I read the code" from "I understand the code."


6. Big-O, , , and "linear time"

Why the topic needs it. The parent note's centrepiece is proving KMP is , not . You cannot read that proof without knowing what these symbols promise. The full accounting method lives in Amortized Analysis.


Prerequisite map

strings and index S of k

substrings

prefix and suffix

proper prefix and suffix

border prefix equals suffix

failure function pi of i

two fingers i and j

KMP matching loop

n m and Big-O

amortized O of n plus m proof

KMP topic

This feeds directly into the parent KMP note. Nearby algorithms reuse these same foundations: Z-algorithm and Rabin-Karp are alternative single-pattern matchers, while Aho-Corasick, Suffix Automaton, and Suffix Array scale the ideas to many patterns and whole-text indexing.


Equipment checklist

Cover the right side and answer aloud; reveal to check.

points at which character?
The first one (indexing starts at zero), and the last is .
What is the empty string ?
The string with zero characters; it is a prefix, a suffix, and a border of every string.
Write the suffix of of length in index notation.
.
What is a proper prefix?
A prefix that is strictly shorter than the whole string — it leaves out at least one character.
What is a border of a string?
A proper prefix that is also a suffix (the same short piece at both ends).
What does store, and what does mean?
The length of the longest border of ; means the only border is the empty string .
Why do we want the longest border, not just any?
It slides the pattern the least, so no real occurrence is skipped.
What are and in the matching loop?
= current index in the text ; = number of pattern characters currently matched.
What range does take, and why is the last matched index ?
is a count running from (nothing matched) up to (full match); since counting starts from zero-matched, the highest matched index is .
What do and stand for?
= text length , = pattern length .
What does (linear time) mean?
Work grows by simply adding the two lengths — much faster than .
What is amortized analysis in one line?
Bounding the total cost over a whole run instead of each step alone.