Minimum Insertion Steps to Make a String Palindrome - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "zzazz"
›
Output:
2
💡 Note:
Insert 'z' at index 2 and 'a' at index 6 to get "zzzazzz" which is palindromic. We can also insert 'a' at index 1 and 'a' at index 3 to get "zaaazz", but that's not optimal.
Example 2 — Already Palindrome
$
Input:
s = "racecar"
›
Output:
0
💡 Note:
The string is already a palindrome, so no insertions are needed.
Example 3 — Single Character
$
Input:
s = "a"
›
Output:
0
💡 Note:
Single character is always a palindrome.
Constraints
- 1 ≤ s.length ≤ 500
- s consists of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Given string that needs to become palindromic
2
Find Strategy
Determine minimum characters needed to insert
3
Result
Return the minimum number of insertion steps
Key Takeaway
🎯 Key Insight: Minimum insertions = string length - longest palindromic subsequence length
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code