Z-function construction: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Compute the longest prefix match starting at each position, reusing the current Z-box. Z[0] is defined as zero.

Reuse the rightmost prefix-matching interval to compute the longest prefix match starting at every position.

Open the 3D visualization

Z-function construction example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Z-function construction

Prefix matching and repetition analysis.

Choosing an alternative

Prefix-function entries describe borders ending at positions instead.

Worked example: input and state changes

Example input

aabcaab

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": []
    }
  2. Step 3: Z[2] = 0; current box [1,1].

    State at this checkpoint
    {
      "values": [
        0,
        1,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        2
      ],
      "input": "aabcaab"
    }
  3. Step 6: Extend prefix match at 4 to length 2.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        0,
        0,
        2,
        0,
        0
      ],
      "active": [
        4
      ],
      "input": "aabcaab"
    }
  4. Step 8: Z[4] = 3; current box [4,6].

    State at this checkpoint
    {
      "values": [
        0,
        1,
        0,
        0,
        3,
        0,
        0
      ],
      "active": [
        4
      ],
      "input": "aabcaab"
    }
  5. Step 11: Construction complete.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        0,
        0,
        3,
        1,
        0
      ],
      "active": [],
      "input": "aabcaab"
    }

Expected final state

Construction complete.

Complexity and input bounds

Family: Strings. Time complexity: O(n).

Space complexity: O(n) Z values and O(1) interval state.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

One string of 1–40 ASCII letters.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

The position-zero convention differs across references; follow this implementation's documented convention.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// a contains 1–40 ASCII letter codes. Writes Z values; Z[0] is defined as zero. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n, output) {
    for (let i = 0; i < n; i++) { output[i] = 0; }
    let left = 0; let right = 0;
    for (let i = 1; i < n; i++) {
        if (i <= right) { output[i] = right - i + 1; if (output[i - left] < output[i]) { output[i] = output[i - left]; } }
        while (i + output[i] < n && a[output[i]] == a[i + output[i]]) { output[i]++; }
        if (i + output[i] - 1 > right) { left = i; right = i + output[i] - 1; }
    }
    return 0;
}

Source

Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.