Longest palindromic subsequence: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Build the longest palindrome length for every interval; skipped characters are allowed.

Extend matching interval endpoints or drop one endpoint and keep the better smaller-interval solution.

Open the 3D visualization

Longest palindromic 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 palindromic subsequence

Finding an order-preserving mirrored selection.

Choosing an alternative

Longest palindromic substring forbids skipped characters.

Worked example: input and state changes

Example input

bbbab

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: Interval table starts empty.

    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
      ],
      "active": [],
      "kind": "matrix",
      "columns": 5,
      "rowLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ],
      "columnLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ]
    }
  2. Step 4: Single character b is a palindrome.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        0,
        1
      ],
      "active": [
        12
      ],
      "kind": "matrix",
      "columns": 5,
      "rowLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ],
      "columnLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ]
    }
  3. Step 8: Interval [1,2] has best palindrome length 2.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        2,
        0,
        0,
        0,
        0,
        1,
        1,
        3,
        0,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        0,
        1
      ],
      "active": [
        7
      ],
      "kind": "matrix",
      "columns": 5,
      "rowLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ],
      "columnLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ]
    }
  4. Step 12: Interval [0,1] has best palindrome length 2.

    State at this checkpoint
    {
      "values": [
        1,
        2,
        0,
        0,
        0,
        0,
        1,
        2,
        2,
        3,
        0,
        0,
        1,
        1,
        3,
        0,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        0,
        1
      ],
      "active": [
        1
      ],
      "kind": "matrix",
      "columns": 5,
      "rowLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ],
      "columnLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ]
    }
  5. Step 16: Longest palindromic subsequence length = 4.

    State at this checkpoint
    {
      "values": [
        1,
        2,
        3,
        3,
        4,
        0,
        1,
        2,
        2,
        3,
        0,
        0,
        1,
        1,
        3,
        0,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        0,
        1
      ],
      "active": [],
      "kind": "matrix",
      "columns": 5,
      "rowLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ],
      "columnLabels": [
        "b",
        "b",
        "b",
        "a",
        "b"
      ]
    }

Expected final state

Longest palindromic subsequence length = 4.

Complexity and input bounds

Family: Dynamic programming. Time complexity: O(n²).

Space complexity: O(n squared) interval 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.

One string of 1–15 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

Initialize one-character intervals correctly; selected characters need not be adjacent.

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
// a contains 1–15 ASCII letter codes. Returns longest palindromic subsequence length; characters may be skipped. 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) {
    let dp = Array(256).fill(0);
    for (let i = n - 1; i >= 0; i--) {
        dp[i * n + i] = 1;
        for (let j = i + 1; j < n; j++) {
            if (a[i] == a[j]) { dp[i * n + j] = 2; if (j > i + 1) { dp[i * n + j] += dp[(i + 1) * n + j - 1]; } }
            else { dp[i * n + j] = dp[(i + 1) * n + j]; if (dp[i * n + j - 1] > dp[i * n + j]) { dp[i * n + j] = dp[i * n + j - 1]; } }
        }
    }
    return dp[n - 1];
}

Source

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