DSAAdvancedguide

Z-Algorithm in Java: Linear Time String Matching

Master the Z-Algorithm in Java. Learn how to find all occurrences of a pattern in a text in linear time using the Z-array, a powerful alternative to KMP.

Sachin SarawgiApril 19, 20263 min read3 minute lesson
Recommended Prerequisites
Big-O Notation in Java: Time and Space Complexity for Interview Problem Solving

The Z-Algorithm is an efficient string matching algorithm that finds all occurrences of a pattern $P$ in a text $T$ in linear time $O(n + m)$.

While many developers learn KMP (Knuth-Morris-Pratt), the Z-Algorithm is often considered more intuitive because it relies on a single array—the Z-array—to store the longest common prefix between the string and its suffixes.

The Core Concept: The Z-Array

For a string $S$, the Z-array $Z$ is an array where $Z[i]$ is the length of the longest common prefix between $S$ and the suffix of $S$ starting at index $i$.

To find a pattern $P$ in text $T$:

  1. Create a concatenated string $S = P + "$" + T$ (where "$" is a character not present in $P$ or $T$).
  2. Compute the Z-array for $S$.
  3. Any index $i$ where $Z[i]$ equals the length of $P$ indicates a match!

Z-Algorithm Implementation in Java

public class ZAlgorithm {
    public int[] calculateZ(String s) {
        int n = s.length();
        int[] z = new int[n];
        int left = 0, right = 0;

        for (int i = 1; i < n; i++) {
            if (i <= right) {
                // If i is within the current [left, right] window, reuse previous values
                z[i] = Math.min(right - i + 1, z[i - left]);
            }
            
            // Try to expand the window manually
            while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) {
                z[i]++;
            }

            // Update the window if we found a match that extends further right
            if (i + z[i] - 1 > right) {
                left = i;
                right = i + z[i] - 1;
            }
        }
        return z;
    }

    public void search(String text, String pattern) {
        String concat = pattern + "$" + text;
        int[] z = calculateZ(concat);
        int pLen = pattern.length();

        for (int i = 0; i < z.length; i++) {
            if (z[i] == pLen) {
                System.out.println("Pattern found at index " + (i - pLen - 1));
            }
        }
    }
}

Z-Algorithm vs. KMP

Feature Z-Algorithm KMP
Data Structure Z-Array (Longest Common Prefix) LPS Array (Longest Prefix Suffix)
Search String $P + $ + T$ Uses $P$ to build table, then scans $T$
Complexity $O(n + m)$ $O(n + m)$
Intuition Easier to visualize as "matching prefixes" Involves "skipping" based on state machine

Why use the Z-Algorithm?

  1. Pattern Matching: Find all occurrences of a string.
  2. String Compression: Find the shortest period of a string.
  3. Prefix/Suffix Problems: Quickly identify common parts of a string and its rotated versions.

Summary

The Z-Algorithm is a powerful, linear-time tool for string manipulation. By using a sliding window to reuse previously calculated prefix information, it avoids redundant character comparisons. Its unified approach to pattern and text processing makes it a favorite for competitive programmers and a great "wow" factor in technical interviews.

📚

Recommended Resources

Java Masterclass — UdemyBest Seller

Comprehensive Java course covering Java 17+, OOP, concurrency, and modern APIs.

View Course
Effective Java, 3rd EditionMust Read

Joshua Bloch's classic guide to writing clear, correct, and efficient Java code.

View on Amazon
Java Concurrency in Practice

The authoritative book on writing thread-safe, concurrent Java programs.

View on Amazon

Practical engineering notes

Get the next backend guide in your inbox

One useful note when a new deep dive is published: system design tradeoffs, Java production lessons, Kafka debugging, database patterns, and AI infrastructure.

No spam. Just practical notes you can use at work.

Sachin Sarawgi

Written by

Sachin Sarawgi

Engineering Manager and backend engineer with 10+ years building distributed systems across fintech, enterprise SaaS, and startups. CodeSprintPro is where I write practical guides on system design, Java, Kafka, databases, AI infrastructure, and production reliability.

Keep Learning

Move through the archive without losing the thread.

Related Articles

More deep dives chosen from shared tags, category overlap, and reading difficulty.

More in DSA

Category-based suggestions if you want to stay in the same domain.