Minimum Insertion Steps to Make a String Palindrome

Hard Dynamic Programming Strings Original on LeetCode

Minimum Insertion Steps to Make a String Palindrome is a hard dynamic programming problem solved with the strings pattern. The best approach, n - lps (via lcs with the reverse), runs in O(n²) time and O(n) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from interval dp directly up.

Problem

Return the minimum number of characters to insert (anywhere) to make s a palindrome.

Examples

Example 1

Input
s = "mbadm"
Output
2
Why
mbdadbm or mdbabdm.

Example 2

Input
s = "leetcode"
Output
5

Constraints

  • 1 <= s.length <= 500; lowercase letters.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 12 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Interval DP directlyO(n²)O(n²)
n - LPS (via LCS with the reverse)O(n²)O(n)

1Interval DP directly

TimeO(n²)
SpaceO(n²)

ins[i][j] = insertions to make s[i..j] a palindrome. Equal ends: ins[i + 1][j - 1]. Otherwise 1 + min(ins[i + 1][j], ins[i][j - 1]).

  1. Fill by increasing substring length.
class Solution {
    public int minInsertions(String s) {
        int n = s.length();
        int[][] dp = new int[n][n];
        for (int len = 2; len <= n; len++)
            for (int i = 0; i + len - 1 < n; i++) {
                int j = i + len - 1;
                dp[i][j] = s.charAt(i) == s.charAt(j)
                        ? dp[i + 1][j - 1]
                        : 1 + Math.min(dp[i + 1][j], dp[i][j - 1]);
            }
        return dp[0][n - 1];
    }
}

2n - LPS (via LCS with the reverse)

TimeO(n²)
SpaceO(n)

Keep the longest palindromic subsequence as the skeleton; every character outside it needs one matching insertion.

  1. lps = LCS(s, reverse(s)) with two rows.
  2. Return n - lps.
class Solution {
    public int minInsertions(String s) {
        String r = new StringBuilder(s).reverse().toString();
        int n = s.length();
        int[] prev = new int[n + 1], cur = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++)
                cur[j] = s.charAt(i - 1) == r.charAt(j - 1) ? prev[j - 1] + 1 : Math.max(prev[j], cur[j - 1]);
            int[] t = prev; prev = cur; cur = t;
        }
        return n - prev[n];
    }
}

Edge cases to test

  • Already a palindrome (0)

Hints

Hint 1

Characters already in the longest palindromic subsequence need no partner. Every other character needs one inserted: answer = n - LPS(s).

FAQ

What is the best time complexity for Minimum Insertion Steps to Make a String Palindrome?

n - LPS (via LCS with the reverse) runs in O(n²) time and O(n) extra space.

Which pattern does Minimum Insertion Steps to Make a String Palindrome use?

It is a dynamic programming problem that uses the strings pattern. Other problems with the same pattern: Edit Distance, Longest Common Subsequence, Longest Palindromic Subsequence.

Is there a brute force solution for Minimum Insertion Steps to Make a String Palindrome?

Yes. Interval DP directly takes O(n²) time and O(n²) space. ins[i][j] = insertions to make s[i..j] a palindrome.

Which edge cases should I test for Minimum Insertion Steps to Make a String Palindrome?

Already a palindrome (0).