Find the minimum number of characters you need to insert into a string so that the whole string becomes a 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.
Compute the smallest number of insertions required so the resulting string is a palindrome.
s.s a palindrome.1 <= |s| is assumed to fit typical interview constraints.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.
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
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.