LeetCode-in-Java

1312. Minimum Insertion Steps to Make a String Palindrome

Hard

Given a string s. In one step you can insert any character at any index of the string.

Return the minimum number of steps to make s palindrome.

A Palindrome String is one that reads the same backward as well as forward.

Example 1:

Input: s = “zzazz”

Output: 0

Explanation: The string “zzazz” is already palindrome we don’t need any insertions.

Example 2:

Input: s = “mbadm”

Output: 2

Explanation: String can be “mbdadbm” or “mdbabdm”.

Example 3:

Input: s = “leetcode”

Output: 5

Explanation: Inserting 5 characters the string becomes “leetcodocteel”.

Constraints:

Solution

public class Solution {
    private int longestPalindrome(String a, String b, int n) {
        int[][] dp = new int[n + 1][n + 1];
        for (int i = 0; i < n + 1; i++) {
            for (int j = 0; j < n + 1; j++) {
                if (i == 0 || j == 0) {
                    dp[i][j] = 0;
                } else if (a.charAt(i - 1) == b.charAt(j - 1)) {
                    dp[i][j] = 1 + dp[i - 1][j - 1];
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[n][n];
    }

    public int minInsertions(String s) {
        int n = s.length();
        if (n < 2) {
            return 0;
        }
        String rs = new StringBuilder(s).reverse().toString();
        int l = longestPalindrome(s, rs, n);
        return n - l;
    }
}