1312. Minimum Insertion Steps to Make a String Palindrome LeetCode Solution

In this guide, you will get 1312. Minimum Insertion Steps to Make a String Palindrome LeetCode Solution with the best time and space complexity. The solution to Minimum Insertion Steps to Make a String Palindrome problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Insertion Steps to Make a String Palindrome solution in C++
  4. Minimum Insertion Steps to Make a String Palindrome solution in Java
  5. Minimum Insertion Steps to Make a String Palindrome solution in Python
  6. Additional Resources
1312. Minimum Insertion Steps to Make a String Palindrome LeetCode Solution image

Problem Statement of Minimum Insertion Steps to Make a String Palindrome

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 do not 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:

1 <= s.length <= 500
s consists of lowercase English letters.

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1312. Minimum Insertion Steps to Make a String Palindrome LeetCode Solution in C++

class Solution {
 public:
  int minInsertions(string s) {
    return s.length() - longestPalindromeSubseq(s);
  }

 private:
  // Same as 516. Longest Palindromic Subsequence
  int longestPalindromeSubseq(const string& s) {
    const int n = s.length();
    // dp[i][j] := the length of LPS(s[i..j])
    vector<vector<int>> dp(n, vector<int>(n));

    for (int i = 0; i < n; ++i)
      dp[i][i] = 1;

    for (int d = 1; d < n; ++d)
      for (int i = 0; i + d < n; ++i) {
        const int j = i + d;
        if (s[i] == s[j])
          dp[i][j] = 2 + dp[i + 1][j - 1];
        else
          dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
      }

    return dp[0][n - 1];
  }
};
/* code provided by PROGIEZ */

1312. Minimum Insertion Steps to Make a String Palindrome LeetCode Solution in Java

class Solution {
  public int minInsertions(String s) {
    return s.length() - longestPalindromeSubseq(s);
  }

  // Same as 516. Longest Palindromic Subsequence
  public int longestPalindromeSubseq(String s) {
    final int n = s.length();
    // dp[i][j] := the length of LPS(s[i..j])
    int[][] dp = new int[n][n];

    for (int i = 0; i < n; ++i)
      dp[i][i] = 1;

    for (int d = 1; d < n; ++d)
      for (int i = 0; i + d < n; ++i) {
        final int j = i + d;
        if (s.charAt(i) == s.charAt(j))
          dp[i][j] = 2 + dp[i + 1][j - 1];
        else
          dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
      }

    return dp[0][n - 1];
  }
}
// code provided by PROGIEZ

1312. Minimum Insertion Steps to Make a String Palindrome LeetCode Solution in Python

class Solution:
  def minInsertions(self, s: str) -> int:
    return len(s) - self._longestPalindromeSubseq(s)

  # Same as 516. Longest Palindromic Subsequence
  def _longestPalindromeSubseq(self, s: str) -> int:
    n = len(s)
    # dp[i][j] := the length of LPS(s[i..j])
    dp = [[0] * n for _ in range(n)]

    for i in range(n):
      dp[i][i] = 1

    for d in range(1, n):
      for i in range(n - d):
        j = i + d
        if s[i] == s[j]:
          dp[i][j] = 2 + dp[i + 1][j - 1]
        else:
          dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])

    return dp[0][n - 1]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.