<p>Given a string <code>s</code>, partition <code>s</code> such that every <span data-keyword="substring-nonempty">substring</span> of the partition is a <span data-keyword="palindrome-string">palindrome</span>.</p>

<p>Return <em>the <strong>minimum</strong> cuts needed for a palindrome partitioning of</em> <code>s</code>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;aab&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> The palindrome partitioning [&quot;aa&quot;,&quot;b&quot;] could be produced using 1 cut.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;a&quot;
<strong>Output:</strong> 0
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;ab&quot;
<strong>Output:</strong> 1
</pre>

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

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

from math import inf

class Solution:

    def cutIntoPalindromicStrings(self, dp, s, i, j, n):
        
        if j == n: 
            return 0 if s[i:j] == s[i:j][::-1] else inf
        
        if dp[i][j] != -1: return dp[i][j]

        else:
            cuts = inf
            cuts = min(cuts, self.cutIntoPalindromicStrings(dp, s, i, j + 1, n))
            if s[i:j] == s[i:j][::-1]: 
                cuts = min(cuts, 1 + self.cutIntoPalindromicStrings(dp, s, j, j+1, n))
            dp[i][j] = cuts
            return cuts

    def minCut(self, s: str) -> int:

        # maximim partition can be the length of the string - 1

        # we know for a any string to be palindromic the reverese should be the same

        # aabbaa

        # i can only partition when i find a pallindromic series
        
        # so whenever i find a palindrome i have an  option to either cut it or just look for a bigger palindromic string

        n = len(s)
        dp = [[-1]*(n+1) for _ in range(n)]

        # for i in range(n+1):
        #     if s[:i] != s[:i][::-1]: dp[0][i] = inf

        return self.cutIntoPalindromicStrings(dp, s, 0, 1, n)


47006