Find the minimum number of characters you need to insert into a string so that the whole string becomes a palindrome.
Minimum Insertions to Make a String Palindrome
Given a string s, return the minimum number of character insertions needed to make s a palindrome.
You may insert any character at any position in the string. You do not need to minimize the final length directly; only the number of inserted characters matters.
A palindrome reads the same forward and backward.
Goal
Compute the smallest number of insertions required so the resulting string is a palindrome.
Input Format
- A single string
s. - The string contains lowercase English letters unless otherwise stated by the platform.
Output Format
- Return an integer: the minimum number of insertions needed to make
sa palindrome.
Constraints
1 <= |s|is assumed to fit typical interview constraints.- A dynamic programming solution is expected.
- Exact platform constraints are not provided here; use standard interview-scale limits.
Example 1
Input
s = "zzazz"
Output
0
Explanation
The string is already a palindrome, so no insertions are needed.
Example 2
Input
s = "mbadm"
Output
2
Explanation
One optimal result is to insert 2 characters to form a palindrome such as mbadabm or mdbabdm.
Show 1 more example
Example 3
Input
s = "leetcode"
Output
5
Explanation
At least 5 insertions are needed to make the string read the same forward and backward.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.