Skip to main content
Back to the DSA atlas
Strings Algorithm Intermediate

Z-Algorithm

Compute how much of the global prefix matches at every position, then reuse the active Z-box to stay linear.

stringpattern-matchingz-algorithmprefix

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

Index 1

Current Z-box

aabaaab

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 i is outside the box, match characters directly
  • if i is 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:

S=P+#+TS = P + \# + T

Then compute the Z-array of S.

Any position where Z[i] = |P| corresponds to an occurrence of the pattern in the text.

Complexity

OperationTime
Build Z-arrayO(n)O(n)

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

ProblemDifficultyKey idea
Find Beautiful Indices in the Given Array I🟡 MediumString occurrence detection and prefix reuse
String Matching with Z-function references🔴 HardDirect Z-array construction
Pattern matching notes🔴 HardCompare 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.

More from Strings

Stay in the same family when you want parallel variations of the same mental model.

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.