Skip to main content

Minimum Insertion Steps to Make a String Palindrome

Problem Statement:​

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:

    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".


βœ… Solution: Memoization, Tabulation, Space Optimized​


βœ… Memoization​

class Solution {
public:
int lcs(string text1, string text2, int ind1, int ind2, vector<vector<int>>& dp){
if(ind1 < 0 || ind2 < 0)
return 0;

if(dp[ind1][ind2] != -1) return dp[ind1][ind2];

if(text1[ind1] == text2[ind2]){
// If characters match, include it in LCS
return dp[ind1][ind2] = 1 + lcs(text1, text2, ind1-1, ind2-1, dp);
} else {
// Else take the max by moving one index back in either string
return dp[ind1][ind2] = max(lcs(text1, text2, ind1-1, ind2, dp),
lcs(text1, text2, ind1, ind2-1, dp));
}
}

int minInsertions(string s) {
int n = s.size();
string t = s;
reverse(t.begin(), t.end());
vector<vector<int>> dp(n, vector<int>(n, -1));

int k = lcs(s, t, n-1, n-1, dp);
return n - k; // Characters not in LPS must be inserted
}
};


βœ… Tabulation​

class Solution {
public:
int minInsertions(string s) {
int n = s.size();
string t = s;
reverse(t.begin(), t.end());

vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));

// Bottom-up LCS between s and its reverse
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(s[i - 1] == t[j - 1])
dp[i][j] = 1 + dp[i-1][j-1];
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}

return n - dp[n][n]; // Min insertions = total length - LPS
}
};


βœ… Space Optimized​

class Solution {
public:
int minInsertions(string s) {
int n = s.size();
string t = s;
reverse(t.begin(), t.end());

vector<int> prev(n + 1, 0);

for(int i = 1; i <= n; i++){
vector<int> curr(n + 1, 0);
for(int j = 1; j <= n; j++){
if(s[i - 1] == t[j - 1])
curr[j] = 1 + prev[j-1];
else
curr[j] = max(prev[j], curr[j-1]);
}
prev = curr;
}

return n - prev[n];
}
};


πŸ“ Required Notes Template​

πŸ“ How It Works​

  • The goal is to make the string a palindrome by inserting the minimum number of characters.
  • The trick is to compute the Longest Palindromic Subsequence (LPS).
  • Once we know the LPS, the remaining characters (i.e., n - LPS) must be inserted to form the palindrome.
  • The LPS is calculated as the LCS of s and reverse(s)`.

🧩 Key Formula / Recurrence​

  • LCS Recurrence:

    if s[i] == t[j]:
    dp[i][j] = 1 + dp[i-1][j-1]
    else:
    dp[i][j] = max(dp[i-1][j], dp[i][j-1])

  • Final Answer:

    minInsertions = length of s - length of LPS


⏱️ Time & Space Complexity​

ApproachTimeSpace
MemoizationO(NΒ²)O(NΒ²) (recursion + DP)
TabulationO(NΒ²)O(NΒ²)
Space OptimizedO(NΒ²)O(N)

⚠️ Edge Cases​

  • Empty string β†’ 0 insertions needed.
  • Already a palindrome β†’ 0 insertions needed.
  • All characters distinct β†’ Insert n - 1 characters.

πŸ’‘ Other Approaches​

ApproachTime
Recursion onlyExponential ❌
DP via LCSO(NΒ²) βœ…


πŸš€ Level Up Your Engineering Mindset

Never Study in Isolation. Join Binary Dose.

Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.

Discord
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚑
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

πŸ’‘Have handwritten notes, cheat-sheets, or want to publish a technical article?
Learn How to Contribute→