<p>Given a string <code>s</code>, you need to partition it into one or more <strong>balanced</strong> <span data-keyword="substring">substrings</span>. For example, if <code>s == &quot;ababcc&quot;</code> then <code>(&quot;abab&quot;, &quot;c&quot;, &quot;c&quot;)</code>, <code>(&quot;ab&quot;, &quot;abc&quot;, &quot;c&quot;)</code>, and <code>(&quot;ababcc&quot;)</code> are all valid partitions, but <code>(&quot;a&quot;, <strong>&quot;bab&quot;</strong>, &quot;cc&quot;)</code>, <code>(<strong>&quot;aba&quot;</strong>, &quot;bc&quot;, &quot;c&quot;)</code>, and <code>(&quot;ab&quot;, <strong>&quot;abcc&quot;</strong>)</code> are not. The unbalanced substrings are bolded.</p>

<p>Return the <strong>minimum</strong> number of substrings that you can partition <code>s</code> into.</p>

<p><strong>Note:</strong> A <strong>balanced</strong> string is a string where each character in the string occurs the same number of times.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;fabccddg&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>We can partition the string <code>s</code> into 3 substrings in one of the following ways: <code>(&quot;fab, &quot;ccdd&quot;, &quot;g&quot;)</code>, or <code>(&quot;fabc&quot;, &quot;cd&quot;, &quot;dg&quot;)</code>.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;abababaccddb&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>We can partition the string <code>s</code> into 2 substrings like so: <code>(&quot;abab&quot;, &quot;abaccddb&quot;)</code>.</p>
</div>

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

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

class Solution:
    def minimumSubstringsInPartition(self, s: str) -> int:
        admissible = [[None for i in range(j)] for j in range(len(s)+1)]
        for i in range(len(s)):
            balancer = Balancer()
            for j in range(i+1, len(s)+1):
                balancer.add(s[j-1])
                admissible[j][i] = balancer.is_balanced()
        
        dp = list(range(len(s), -1, -1))
        for i in range(len(s), -1, -1):
            for j in range(i, len(s)):
                if not admissible[j+1][i]:
                    continue
                dp[i] = min(dp[i], 1 + dp[j+1])
        return dp[0]


class Balancer:
    def __init__(self):
        self.counts = collections.defaultdict(int)
        self.freqs = collections.defaultdict(int)
        self.last = None
    
    def add(self, x):
        if self.counts[x] > 0:
            self.freqs[self.counts[x]] -= 1
        self.counts[x] += 1
        self.freqs[self.counts[x]] += 1
        self.last = x
    
    def is_balanced(self):
        if self.last is None:
            return True
        return self.freqs[self.counts[self.last]] == len(self.counts)
20800