Longest common subsequence: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Build prefix LCS lengths, then backtrack to reconstruct one longest common subsequence.

Matching final prefix characters extend an inner solution; otherwise discard one final character from either prefix.

Open the 3D visualization

Longest common subsequence example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Longest common subsequence

Order-preserving similarity and basic diff construction.

Choosing an alternative

Common substring requires contiguity; edit distance assigns edit costs.

Worked example: input and state changes

Example input

ABCBDAB | BDCABA

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: Base row and column are zero.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [],
      "kind": "matrix",
      "columns": 7,
      "rowLabels": [
        "∅",
        "A",
        "B",
        "C",
        "B",
        "D",
        "A",
        "B"
      ],
      "columnLabels": [
        "∅",
        "B",
        "D",
        "C",
        "A",
        "B",
        "A"
      ]
    }
  2. Step 13: Compare C and B; LCS length for these prefixes is 1.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        1,
        0,
        1,
        1,
        1,
        1,
        2,
        2,
        0,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        22
      ],
      "kind": "matrix",
      "columns": 7,
      "rowLabels": [
        "∅",
        "A",
        "B",
        "C",
        "B",
        "D",
        "A",
        "B"
      ],
      "columnLabels": [
        "∅",
        "B",
        "D",
        "C",
        "A",
        "B",
        "A"
      ]
    }
  3. Step 26: Compare D and D; LCS length for these prefixes is 2.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        1,
        0,
        1,
        1,
        1,
        1,
        2,
        2,
        0,
        1,
        1,
        2,
        2,
        2,
        2,
        0,
        1,
        1,
        2,
        2,
        3,
        3,
        0,
        1,
        2,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        37
      ],
      "kind": "matrix",
      "columns": 7,
      "rowLabels": [
        "∅",
        "A",
        "B",
        "C",
        "B",
        "D",
        "A",
        "B"
      ],
      "columnLabels": [
        "∅",
        "B",
        "D",
        "C",
        "A",
        "B",
        "A"
      ]
    }
  4. Step 38: Compare B and D; LCS length for these prefixes is 2.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        1,
        0,
        1,
        1,
        1,
        1,
        2,
        2,
        0,
        1,
        1,
        2,
        2,
        2,
        2,
        0,
        1,
        1,
        2,
        2,
        3,
        3,
        0,
        1,
        2,
        2,
        2,
        3,
        3,
        0,
        1,
        2,
        2,
        3,
        3,
        4,
        0,
        1,
        2,
        0,
        0,
        0,
        0
      ],
      "active": [
        51
      ],
      "kind": "matrix",
      "columns": 7,
      "rowLabels": [
        "∅",
        "A",
        "B",
        "C",
        "B",
        "D",
        "A",
        "B"
      ],
      "columnLabels": [
        "∅",
        "B",
        "D",
        "C",
        "A",
        "B",
        "A"
      ]
    }
  5. Step 51: LCS length = 4; subsequence: BCBA.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        1,
        0,
        1,
        1,
        1,
        1,
        2,
        2,
        0,
        1,
        1,
        2,
        2,
        2,
        2,
        0,
        1,
        1,
        2,
        2,
        3,
        3,
        0,
        1,
        2,
        2,
        2,
        3,
        3,
        0,
        1,
        2,
        2,
        3,
        3,
        4,
        0,
        1,
        2,
        2,
        3,
        4,
        4
      ],
      "active": [],
      "kind": "matrix",
      "columns": 7,
      "rowLabels": [
        "∅",
        "A",
        "B",
        "C",
        "B",
        "D",
        "A",
        "B"
      ],
      "columnLabels": [
        "∅",
        "B",
        "D",
        "C",
        "A",
        "B",
        "A"
      ],
      "subsequence": "BCBA"
    }

Expected final state

LCS length = 4; subsequence: BCBA.

Complexity and input bounds

Family: Dynamic programming. Time complexity: O(nm).

Space complexity: O(nm) reconstruction table.

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.

First string | second string; up to 12 letters each, including empty strings.

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

Subsequences may skip characters and ties can produce several valid reconstructions.

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
12
13
14
15
16
17
18
// first and second are ASCII character-code arrays, each length ≤ 12. Writes one longest common subsequence as character codes; returns its length. 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(first, n, second, m, output) {
    let dp = Array(256).fill(0); let columns = m + 1;
    for (let i = 1; i <= n; i++) {
        for (let j = 1; j <= m; j++) {
            let k = i * columns + j;
            if (first[i - 1] == second[j - 1]) { dp[k] = dp[k - columns - 1] + 1; }
            else { dp[k] = dp[k - columns]; if (dp[k - 1] > dp[k]) { dp[k] = dp[k - 1]; } }
        }
    }
    let length = dp[n * columns + m]; let index = length; let row = n; let column = m;
    while (row > 0 && column > 0) {
        if (first[row - 1] == second[column - 1]) { output[--index] = first[row - 1]; row--; column--; }
        else if (dp[(row - 1) * columns + column] >= dp[row * columns + column - 1]) { row--; }
        else { column--; }
    }
    return length;
}

Source

Hirschberg presents a linear-space LCS method. The playground intentionally displays a full quadratic dynamic-programming table.