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

"""
the minimum length of the substring that can be replaced with any other string of the same length to make s balanced. 
 ->即找当window外每个char都不满足balanced的时候，window的最小长度
   window外面的char改不了，window里面的char怎么改都可以 -> 要让string balanced
   
**window里面**每走一步，就要sMap[s[i]] -= 1，这是因为window里面的char占用了总freq，所以从总freq里面减掉
这时候，当window外面的char freq低于limit，说明*窗口外面*已经不能满足balanced string；这时候停止，并记录窗口大小，移动
试着把窗口缩小

因为如果窗口外的字母超过了limit，窗口里面怎么换都不行，因为窗口外始终有超出要求次数的字母的, 所以是用<=来判断
"""

#Time O(n)
    #freq = Counter(s) 这一步需要遍历字符串 s，时间复杂度是 O(n)。
    #主循环for loop运行了 n 次，每次循环内的 while 循环最多也会执行 n 次（虽然 while 循环的总次数实际上远少于 n^2 次，因为每次成功执行后 
    #start 会增大，使得 while 循环的条件更难满足），所以整体时间复杂度为 O(n)
    
#Space O(1) 固定4种字母
class Solution:
    def balancedString(self, s: str) -> int:
        #len(s) % 4 has to == 0  len(s) is a multiple of 4.
        
        freq = Counter(s)
        result = math.inf
        n = len(s)
        limit = n / 4
        start = 0
    
        #start < n是为了避免一开始就balanced的string, 那样start可以无限++; 也可以一开始就判断这个conner case
        # if(all(freq[x] <= limit for x in "QWER")): return 0
           
        for i in range(n):
            freq[s[i]] -= 1
            
                 #start < n是为了避免一开始就balanced的string, 那样start可以无限++; 也可以一开始就判断这个conner case
                                #window外面的char freq低于limit，说明*窗口外面*已经不能满足balanced string
            while(start < n and all(freq[x] <= limit for x in "QWER")):
                result = min(result, i - start + 1)
                freq[s[start]] += 1
                start += 1
        

        return result


315