Minimum Insertion Steps to Make a String Palindrome

problem [https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/ 1312. Minimum Insertion Steps to Make a String Palindrome].

Problem description
Given a string. In one step you can insert any character at any index of the string.

Return the minimum number of steps to make  palindrome.

A Palindrome String is one that reads the same backward as well as forward.


 * Example 1
 * Input: s = "zzazz"
 * Output: 0
 * Explanation: The string "zzazz" is already palindrome we don't 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".


 * Example 4
 * Input: s = "g"
 * Output: 0


 * Example 5
 * Input: s = "no"
 * Output: 1


 * Constraints
 * All characters of  are lower case English letters.
 * All characters of  are lower case English letters.

Problem analysis
One important point to notice that this problem is built upon the Longest Palindromic Subsequence problem. Once we find out the longest palindromic subsequence, we will be able to insert the remaining characters in-between the palindromic subsequence to make the complete palindrome. So we can use dynamic programming to solve the problem.

See Longest Palindromic Subsequence for the complete analysis.

The only difference is that the result equals the input string's length minus the length of the longest palindromic subsequence.

Complexity analysis

 * Space complexity
 * Building the  table takes O(n2) space. So the total space complexity is O(n2).


 * Time complexity
 * Computing each cell requires a constant amount of computation. So the total time complexity is O(n2).