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

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

        '''
        
        for every 'i' pos check if this split leads to optimum result
        abcbab


        '''

        palin = set()

        def populate_palin(s):
            n = len(s)
            for o_mid in range(n):
                p1, p2 = o_mid, o_mid
                while p1 >=0 and p2 < n and s[p1] == s[p2]:
                    palin.add((p1,p2))
                    p1 -= 1
                    p2 += 1
            for e_mid in range(n):
                p1, p2 = e_mid, e_mid + 1
                while p1 >=0 and p2 < n and s[p1] == s[p2]:
                    palin.add((p1,p2))
                    p1 -= 1
                    p2 += 1


        '''
        def palin(s, s_idx, e_idx):

            key = (s_idx, e_idx)
            if key not in palin_dp:
                res = True
                while s_idx < e_idx:
                    if (s_idx, e_idx) in palin_dp:
                        res = palin_dp[(s_idx, e_idx)]
                        break
                    if s[s_idx] != s[e_idx]:
                        res = False
                        break

                    s_idx += 1
                    e_idx -= 1

                palin_dp[key] = res
            
            return palin_dp[key]
        '''

        self.dp_tbl = {}
        populate_palin(s)

        def partition(s_idx, n):
            if s_idx == n:
                return -1

            key = s_idx

            if key not in self.dp_tbl:
                best = n - s_idx # all individual chars is the worst possible answer

                for e_idx in range(s_idx, n):
                    #if palin(s, s_idx, e_idx):
                    if (s_idx, e_idx) in palin:
                        min_cuts = 1 + partition(e_idx + 1, n)
                        best = min(best, min_cuts)
            
                self.dp_tbl[key] = best
            
            return self.dp_tbl[key]

        n = len(s)
        return partition(0, n)

















                    
401841