<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(self, s: str) -> int:
        rev_s = s[::-1]
        if s == rev_s:
            return 0
        l = len(s)

        mem = {}

        # prev = [0]*(l + 1)
        # curr = [0]*(l + 1)

        # for i in range(l + 1):
        #     for j in range(l + 1):
        #         if i != 0 and j != 0:
        #             if s[i - 1] == rev_s[j - 1]:
        #                 curr[j] = 1 + prev[j-1]
        #             else:
        #                 curr[j] = max(curr[j-1], prev[j])

        #     prev = copy.copy(curr)


        # return l - max(curr)
            


        # dp = [[0]*(l+1) for _ in range(l+1)]

        # for i in range(l+1):
        #     for j in range(l+1):
        #         if i == 0 or j == 0:
        #             dp[i][j] = 0
        #         elif s[i - 1] == rev_s[j - 1]:
        #             dp[i][j] = 1 + dp[i - 1][j - 1]
        #         else:
        #             dp[i][j] = max(dp[i][j-1], dp[i-1][j])

        # return l - dp[l][l]
        
        def longest_common(s, rev_s):
            if not s or not rev_s:
                return 0
            if s == rev_s:
                return len(s)

            if (s, rev_s) in mem:
                return mem[(s, rev_s)]

            if (rev_s, s) in mem:
                return mem[(rev_s, s)]
            
            if s[-1] == rev_s[-1]:
                result =  1 + longest_common(s[:-1], rev_s[:-1])
                mem[(s, rev_s)] = result
                return result
            
            result =   max(longest_common(s[:-1], rev_s), longest_common(s, rev_s[:-1]))
            mem[(s, rev_s)] = result
            return result

        l_c = longest_common(s, rev_s)

        del mem
        return l - l_c




68559