Prefix-function construction: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Compute the longest proper prefix that is also a suffix of every string prefix.

Store the longest proper border for every prefix and follow earlier border links after a mismatch.

Open the 3D visualization

Prefix-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 Prefix-function construction

KMP preprocessing and repeated-prefix analysis.

Choosing an alternative

Z-values measure prefix matches starting at each position.

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 2: Fall back to border length 0.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        2
      ],
      "input": "aabcaab"
    }
  3. Step 4: Prefix function at 3 = 0.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        3
      ],
      "input": "aabcaab"
    }
  4. Step 6: Prefix function at 5 = 2.

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

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

Expected final state

Construction complete.

Complexity and input bounds

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

Space complexity: O(n) prefix values.

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

A proper border cannot equal the full prefix; one-character fallback instead of border links can lose linear behavior.

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 the length of the longest proper prefix-suffix for each prefix. 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) {
    output[0] = 0;
    for (let i = 1; i < n; i++) {
        let j = output[i - 1];
        while (j > 0 && a[i] != a[j]) { j = output[j - 1]; }
        if (a[i] == a[j]) { j++; }
        output[i] = j;
    }
    return 0;
}

Source

The prefix/failure-function method is part of KMP preprocessing. The playground exposes it as a separate view.