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

class Solution:
    def balancedString(self, s: str) -> int:
        n = len(s)
        num = n//4

        counter_left = collections.defaultdict(int)
        left_idx1 = n-1
        for i in range(n):
            if counter_left[s[i]] == num:
                left_idx1 = i-1
                break
            counter_left[s[i]] += 1

        right_idx1 = left_idx1
        for j in range(n-1, left_idx1, -1):
            if counter_left[s[j]] == num:
                right_idx1 = j
                break
            counter_left[s[j]] += 1

        counter_right = collections.defaultdict(int)
        right_idx2 = 0
        for i in range(n-1, -1, -1):
            if counter_right[s[i]] == num:
                right_idx2 = i+1
                break
            counter_right[s[i]] += 1

        left_idx2 = right_idx2

        for j in range(right_idx2):
            if counter_right[s[j]] == num:
                left_idx2 = j
                break
            counter_right[s[j]] += 1

        ans = right_idx1 - left_idx1

        while right_idx1 >= right_idx2 and left_idx1 >= left_idx2:
            counter_left[s[left_idx1]] -= 1
            left_idx1 -= 1
            while counter_left[s[right_idx1]] < num:
                counter_left[s[right_idx1]] += 1
                right_idx1 -= 1
            
            ans = min(ans, right_idx1-left_idx1)

        return ans



112