<p>You are given a string s of length <code>n</code> containing only four kinds of characters: <code>&#39;Q&#39;</code>, <code>&#39;W&#39;</code>, <code>&#39;E&#39;</code>, and <code>&#39;R&#39;</code>.</p>

<p>A string is said to be <strong>balanced</strong><em> </em>if each of its characters appears <code>n / 4</code> times where <code>n</code> is the length of the string.</p>

<p>Return <em>the minimum length of the substring that can be replaced with <strong>any</strong> other string of the same length to make </em><code>s</code><em> <strong>balanced</strong></em>. If s is already <strong>balanced</strong>, return <code>0</code>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;QWER&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> s is already balanced.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;QQWE&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> We need to replace a &#39;Q&#39; to &#39;R&#39;, so that &quot;RQWE&quot; (or &quot;QRWE&quot;) is balanced.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;QQQW&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can replace the first &quot;QQ&quot; to &quot;ER&quot;. 
</pre>

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

<ul>
	<li><code>n == s.length</code></li>
	<li><code>4 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>n</code> is a multiple of <code>4</code>.</li>
	<li><code>s</code> contains only <code>&#39;Q&#39;</code>, <code>&#39;W&#39;</code>, <code>&#39;E&#39;</code>, and <code>&#39;R&#39;</code>.</li>
</ul>

from collections import defaultdict
class Solution:
    def balancedString(self, s: str) -> int:
        def twoPointers():
            charCounts = defaultdict(int)
            for c in s:
                charCounts[c] += 1
            n = len(s)
            b = n // 4
            i, j = 0, 0
            ret = n
            currCounts = charCounts.copy()
            while i < n:
                if currCounts[s[i]] < b or j >= n:
                    if i != j:
                        currCounts[s[i]] += 1
                    i += 1
                    if j < i:
                        j = i
                else:
                    currCounts[s[j]] -= 1
                    j += 1

                update = True
                for v in currCounts.values():
                    if v > b:
                        update = False
                        break
                if update:
                    ret = min(ret, j-i)

                    
            return ret

        def brute():
            charCounts = defaultdict(int)
            for c in s:
                charCounts[c] += 1
            n = len(s)
            b = n / 4

            toReplaceCounts = {}
            for k, v in charCounts.items():
                if v > b:
                    toReplaceCounts[k] = v - b
            
            if not toReplaceCounts:
                return 0
            
            if len(toReplaceCounts.keys()) == 1 and next(iter(toReplaceCounts.values())) == (n-b):
                return int(b * 3)

            # else, we have stuff to replace
            # sum(values in toReplaceCounts)
            ret = n
            tots = sum(toReplaceCounts.values())
            for i, c in enumerate(s):
                currCount = toReplaceCounts.copy()
                tot = tots
                currRet = 0
                valid = False
                for j in range(i, n):
                    currRet += 1
                    # check against currCount each character 
                    # if character is in currCount, -1
                    # if character 0, ignore
                    # if all characters 0, break
                    # ret = min(ret, currRet)
                    if s[j] in currCount and currCount[s[j]] != 0:
                        currCount[s[j]] -= 1
                        tot -= 1
                    if tot <= 0:
                        valid = True
                        break
                if valid:
                    ret = min(ret, currRet)
            return ret
        return twoPointers()


189