<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 collections import defaultdict, deque

class Solution:
    def minCut(self, s: str) -> int:
        def potentialSubstrings(s):
            potential = defaultdict(lambda:[])
            for i in range(len(s)):
                index = 0
                while i-index >= 0 and i+index < len(s) and s[i+index] == s[i-index]:
                    potential[i-index].append(i+index)
                    index += 1
                index = 0
                while i-index >= 0 and i+index+1 < len(s) and s[i+index+1] == s[i-index]:
                    potential[i-index].append(i+index+1)
                    index += 1
            return potential
        potential = potentialSubstrings(s)
        neighbors = deque(potential[0])
        visited = set()
        ans = 0
        while neighbors:
            ans += 1
            x = len(neighbors)
            for i in range(x):
                curr= neighbors.popleft()
                if curr+1 >= len(s):
                    return ans-1
                if curr in visited:
                    continue
                visited.add(curr)
                for j in range(len(potential[curr+1])):
                    neighbors.append(potential[curr+1][j])

        # def recurse(potential, currIndex, s):
        #     if currIndex >= len(s):
        #         return 0
        #     if len(potential[currIndex]) == 0:
        #         return float('inf')
        #     ans = float('inf')
        #     for i in range(len(potential[currIndex])):
        #         x = recurse(potential, potential[currIndex][i]+1, s)
        #         ans = min(ans, x+1)
        #     return ans
        # return recurse(potential, 0, s)-1
108420