3.8.4 · D1String Algorithms

Foundations — Z-algorithm — Z-array construction, O(n+m)

1,856 words8 min readBack to topic

Before you can read the parent note, you must own every piece of notation it throws at you. Below, each symbol is built from nothing: plain words → the picture → why the topic needs it. Read top to bottom; each one leans on the previous.


1. A string, and what "index" means

Picture a row of boxes, each holding one letter, with a small number written under each box.

Figure — Z-algorithm — Z-array construction, O(n+m)
  • means "the character in slot 0" — the very first one.
  • means "the character in slot ".

2. Substring — a contiguous slice

The key word is contiguous — no gaps, no skipping. aab is a substring of aabxaa; abx (in that order, adjacent) is too; but axa is not because the letters aren't next to each other.

Figure — Z-algorithm — Z-array construction, O(n+m)

3. Prefix — the string's signature

Picture sliding a cut-line from left to right: everything to the left of the cut is a prefix. The prefix of length is (it has characters, occupying slots through ).

Figure — Z-algorithm — Z-array construction, O(n+m)

4. Equality of two substrings

If even one pair of letters differs, the whole equality is false. This is exactly a match. When we say " matches the prefix ", we mean all pairs agree.


5. The Z-value

Now we can build the star symbol of the topic.

Let's earn every piece of that formula, left to right:

  • is a candidate match-length we're testing.
  • is the prefix of length (from section 3).
  • is the slice of length that starts at (from section 2 — note it has boxes because ).
  • The "" asks: are those two slices identical (section 4)?
  • says: among all that make them equal, take the biggest. That's the longest photocopy of the beginning that starts here.
Figure — Z-algorithm — Z-array construction, O(n+m)

6. The window — the "Z-box"

The width of that box is (section 2's counting rule again).


7. The mirror index

Picture the box laid on top of the prefix — they're identical copies. Slot sits some distance from the box's left edge ; slot sits that same distance from the prefix's left edge (slot ). So is 's reflection in the "photocopy."

Because the box equals the prefix, — a fact, not a guess. That fact is what lets us copy the already-computed for free.


The prerequisite map

String s and 0-indexing

Substring s a to b

Length b minus a plus 1

Prefix starts at slot 0

Equality match of slices

Z value at i

Window l to r the Z-box

Mirror index i minus l

Min cap and while extend

Z algorithm O of n plus m

Each foundation feeds the next; the two arrows into "Z value" (prefix + equality) and the two into "min cap" (mirror + counting) are the load-bearing joins.


Where this sits in the bigger picture

The Z-array is one of several ways to encode "how does a string repeat inside itself." Its close cousin is the KMP failure function (which measures longest border instead of longest prefix-match). For a full trace with the same running example in Hindi/English mix, see 3.8.04 Z-algorithm — Z-array construction, O(n+m) (Hinglish). The linear-time speed claim rests on Amortized Analysis (the " only moves right" argument). Other tools for the same family of problems: String Hashing, Suffix Array, Suffix Automaton, and for palindromes the mirror-trick reappears in Manacher's Algorithm. All of these build on the same foundations above, so mastering them here pays off everywhere.

Once you're comfortable with these symbols, head to the parent: 3.8.4 Z-algorithm — Z-array construction, O(n+m).


Equipment checklist

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

What does mean, and where does counting start?
The character in slot ; counting starts at , so is the first character.
How many characters are in the substring ?
Exactly (both ends included).
What is a prefix of ?
A substring that starts at slot — the "beginning"; the prefix of length is .
What does it mean for two slices to be equal?
Same length and identical character-by-character; every matching pair of letters agrees.
State in one sentence.
The length of the longest substring starting at that also equals a prefix of .
Why do we set ?
The whole string trivially matches itself, so would just be — useless — and we never use it.
What is the Z-box ?
The already-verified interval matching the prefix () whose right edge reaches farthest right.
What is the mirror index and why does ?
; because inside the box the string is an exact copy of the prefix, so slot equals its reflection near the start.
What does prevent?
Claiming matches past the right edge that were never verified — it caps trust at the box boundary.