Z-Algorithm
Compute how much of the global prefix matches at every position, then reuse the active Z-box to stay linear.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
Index 1
Current Z-box
Known Z values
z[0]
0
z[1]
1
z[2]
-
z[3]
-
z[4]
-
z[5]
-
z[6]
-
Reuse the active prefix box
At index 1, match one character with the prefix, so the Z-box becomes [1, 1].
Family
Strings
Prefix-aware structures and text-centric search strategies.
Builds on
Foundational
You can start here without another page first.
Unlocks
1 next topic
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
1
This topic appears in curated progressions so you can keep moving without guessing the next step.
Problem
You want to know, for every index i, how many characters starting at i match the prefix of the string.
That array of answers is called the Z-array.
It is useful on its own, and it also gives a clean route to pattern matching.
Core idea
Maintain the current interval [L, R] whose substring matches the prefix.
When processing a new index i:
- if
iis outside the box, match characters directly - if
iis inside the box, reuse earlier Z-values to skip work - extend the box only when necessary
That reuse is why the algorithm stays linear.
Why it matters
KMP stores prefix/suffix overlap for pattern prefixes. Z-algorithm stores prefix-match length for every position.
They solve related problems from different angles:
- KMP is centered on mismatch fallback during search
- Z is centered on prefix matching over the whole string
Both are important because they teach the same broader lesson: string structure can replace repeated character-by-character restarts.
Pattern matching trick
To search for pattern P inside text T, build:
Then compute the Z-array of S.
Any position where Z[i] = |P| corresponds to an occurrence of the pattern in the text.
Complexity
| Operation | Time |
|---|---|
| Build Z-array |
Key takeaways
- Z-array tells you how strongly every suffix prefix-aligns with the whole string
- The Z-box
[L, R]is the reuse mechanism that keeps the algorithm linear - Z-algorithm is a serious core string tool, not just a contest trick
- KMP and Z are worth learning together because each clarifies the other
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Find Beautiful Indices in the Given Array I | 🟡 Medium | String occurrence detection and prefix reuse |
| String Matching with Z-function references | 🔴 Hard | Direct Z-array construction |
| Pattern matching notes | 🔴 Hard | Compare KMP and Z-based matching |
Relation to other topics
- KMP captures a similar prefix-reuse idea in a fallback table
- Aho-Corasick generalizes structured fallback to many patterns at once
What this enables
Once the current idea feels natural, these are the most useful next jumps.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
KMP (Knuth-Morris-Pratt)
Search for a pattern in linear time by reusing information about its own prefixes instead of restarting after mismatches.
Rabin-Karp
Search with rolling hashes so most windows can be rejected with arithmetic instead of full character comparisons.
Aho-Corasick
Match many patterns in one pass by augmenting a trie with failure links. This is multi-pattern search done at scale.
Manacher
Find the longest palindromic substring in linear time by reusing information from the rightmost known palindrome.
More from Strings
Stay in the same family when you want parallel variations of the same mental model.
KMP (Knuth-Morris-Pratt)
Search for a pattern in linear time by reusing information about its own prefixes instead of restarting after mismatches.
Rabin-Karp
Search with rolling hashes so most windows can be rejected with arithmetic instead of full character comparisons.
Trie (Prefix Tree)
A tree-like data structure for efficient string storage and prefix-based retrieval. Essential for autocomplete, spell checking, and word search problems.
Aho-Corasick
Match many patterns in one pass by augmenting a trie with failure links. This is multi-pattern search done at scale.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
String matching
Move from prefix reuse and rolling hashes to suffix-based indexing and automata.
From the blog
Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.