<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 Counter
class Solution:
    def balancedString(self, s: str) -> int:
        """
        s: str: len(s) = n
        s consists of chars {Q, W, E, R}
        Constraint: 
            Balance = True if Count{Q,W,E,R} == n/4
        Objective:
            Min len of substring when replaced makes the string balanced
            If already balanced --> 0

        Assumptions:
        1. n >= 4
        2. n%4 == 0


        O(n^2)
        for every i in range(n):
            for every j in range(n):
                window = n[i:j+1]
                Check if replacing the window makes the string balanced

        
        count_dict = Counter(s)
        left=0
        1. Increase the window to the right at each step: Reduce the chars in the window from the counter
            Keep reducing the window from the left until the string gets unbalanced:
                Keep track of minimum window  


        Example:
        Q:2
        W:4
        E:1
        R:1

      
        """

        def isBalanced(count, limit):

            return count['Q'] <= limit and count['W'] <= limit and count['E'] <= limit and count['R'] <= limit

        count = Counter(s)
        left = 0
        min_window_size = float('inf')

        for right in range(len(s)):
            count[s[right]]-=1 # Replace chars inside the window

            while left < len(s) and isBalanced(count, len(s)/4):
                window_size = right - left + 1
                min_window_size = min(window_size, min_window_size)
                count[s[left]]+=1
                left+=1
        
        return min_window_size


        
203