Minimum Insertion Steps to Make a String Palindrome - Problem

Imagine you have a string that's almost a palindrome, but not quite there yet. Your mission? Transform it into a perfect palindrome by strategically inserting characters!

Given a string s, you can insert any character at any position in the string. Your goal is to find the minimum number of insertions needed to make the string read the same forwards and backwards.

What makes this challenging? You need to be smart about which characters to insert and where to place them. For example:

  • "zzazz" is already a palindrome → 0 insertions needed
  • "mbadm" can become "mbdadbm"2 insertions needed
  • "leetcode" requires more strategic planning → 5 insertions needed

This problem tests your understanding of dynamic programming and string manipulation, making it a favorite in technical interviews!

Input & Output

example_1.py — Basic Case
$ Input: s = "zzazz"
Output: 0
💡 Note: The string "zzazz" is already a palindrome, so no insertions are needed.
example_2.py — Simple Transformation
$ Input: s = "mbadm"
Output: 2
💡 Note: We can transform "mbadm" to "mbdadbm" by inserting 'd' after 'b' and 'b' before the last 'm'. This creates a palindrome with just 2 insertions.
example_3.py — Complex Case
$ Input: s = "leetcode"
Output: 5
💡 Note: One way is to transform "leetcode" to "etelcecotele" by strategically inserting 5 characters to make it palindromic.

Constraints

  • 1 ≤ s.length ≤ 500
  • s consists of lowercase English letters only
  • Time limit: 1 second per test case

Visualization

Tap to expand
Palindrome Transformation ProcessOriginal: "mbadm"mbadm✓ Match!Step 1: Outer characters matchm == m, so solve for inner "bad"bad✗ No MatchStep 2: Process "bad"b ≠ d, try both options:Option 1: Insert 'd' at start → "dbad"Option 2: Insert 'b' at end → "badb"dbadb+2 insertionsFinal Result: "mbdadbm"mbdadbmPerfect Palindrome!+1+1DP Table InsightThe 2D DP table efficiently computes:• dp[i][j] = min insertions for s[i...j]• If s[i] == s[j]: dp[i][j] = dp[i+1][j-1]• Else: dp[i][j] = 1 + min(dp[i+1][j], dp[i][j-1])Time: O(n²), Space: O(n²)Answer found at dp[0][n-1]Orange characters are insertions needed to create perfect symmetry
Understanding the Visualization
1
Identify Mismatches
Find positions where characters don't mirror each other
2
Strategic Insertion
Insert characters to create perfect symmetry
3
Minimize Operations
Use DP to find the minimum number of insertions needed
Key Takeaway
🎯 Key Insight: This problem is equivalent to finding the Longest Palindromic Subsequence (LPS). The minimum insertions needed equals string length minus LPS length, solved efficiently using dynamic programming.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
42.0K Views
Medium-High Frequency
~18 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen