<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:
        if len(s) < 2:
            return 0
        storage = [-1 for _ in range(len(s))]
        palindrome_storage = set()
        for sum_i_k in range(0, len(s) * 2 - 1):
            i = sum_i_k // 2
            k = sum_i_k - i
            while i >= 0 and k <= len(s) - 1:
                if s[i] == s[k]:
                    palindrome_storage.add((i, k))
                else:
                    break
                i -= 1
                k += 1
        # print(palindrome_storage)
        def is_palindrome(i, k):
            return (i, k) in palindrome_storage
        #     while i <= k:
        #         if s[i] != s[k]:
        #             return False
        #         i += 1
        #         k -= 1
        #     return True
        def recursiveMinCut(i):
            best = 10 ** 9 + 5
            if storage[i] > -1:
                return storage[i]
            # if i == len(s) - 1:
            #     return 1
            for j in range(i + 1, len(s) + 1):
                if is_palindrome(i, j - 1):
                    # print('in')
                    cur = 1
                    if j < len(s):
                        cur += recursiveMinCut(j)
                    if cur < best:
                        best = cur
            storage[i] = best
            return best
        return recursiveMinCut(0) - 1

388194