<p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>

<p>Return <em>the minimum number of steps</em> to make <code>s</code>&nbsp;palindrome.</p>

<p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;zzazz&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string &quot;zzazz&quot; is already palindrome we do not need any insertions.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;mbadm&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be &quot;mbdadbm&quot; or &quot;mdbabdm&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;leetcode&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes &quot;leetcodocteel&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 500</code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class Solution:
    def minInsertions_MLE(self, s: str) -> int:
        """
        search space: <= 2 * len(s)
        anchored [f1....f1] they need to be repeated.
        insertion is hard, can we delete?

        *** find the LIS which is pal and use n - LIS for insertion
        EXAMPLE: ete --> in leetcode:  8 - 3 = 5
        letter: freq --> if used, decrease the freq, to see all cands of sub seq for palindrome, backtracking
        """  
        @cache      
        def dfs(l, r, cur):
            if l > r: return 0
            elif l == r: return 1
            else:
                ans = 1
                j = r
                while j >= l:
                    if s[j] == s[l]: break
                    j -= 1
                if s[j] == s[l]:
                    #found
                    d = 2 if j != l else 1
                    ans = max(ans, d + dfs(l+1, j-1, s[l] + cur + s[l]))
                ans = max(ans, dfs(l + 1, r, cur), dfs(l, r - 1, cur))
                return ans
        
        lis = dfs(0, len(s)-1, '')
        print(lis)
        return len(s) - lis



    def minInsertions(self, s: str) -> int:
        """
        search space: <= 2 * len(s)
        anchored [f1....f1] they need to be repeated.
        insertion is hard, can we delete?

        *** find the LIS which is pal and use n - LIS for insertion
        EXAMPLE: ete --> in leetcode:  8 - 3 = 5
        letter: freq --> if used, decrease the freq, to see all cands of sub seq for palindrome, backtracking
        """  
        @cache      
        def dfs(l, r):
            if l > r: return 0
            elif l == r: return 1
            else:
                ans = 1
                j = r
                while j >= l:
                    if s[j] == s[l]: break
                    j -= 1
                if s[j] == s[l]:
                    #found
                    d = 2 if j != l else 1
                    ans = max(ans, d + dfs(l+1, j-1))
                ans = max(ans, dfs(l + 1, r), dfs(l, r - 1))
                return ans
        
        lis = dfs(0, len(s)-1)
        print(lis)
        return len(s) - lis

179050