#1312

Minimum Insertion Steps to Make a String Palindrome

specialist · 940 · lc hard +32 · verified · 73.7% accepted · 5,567 likes · top 84%

Description

Given a string s, you may insert any character at any position in a single step. Find the minimum number of insertions needed to make s a palindrome — a string that reads the same forwards and backwards.

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

Code

1
2
3