<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:
        # Step 1: Count the occurrences of each character
        count = {'Q': 0, 'W': 0, 'E': 0, 'R': 0}
        for char in s:
            count[char] += 1
        
        # Calculate the target count for each character to be balanced
        n = len(s)
        target_count = n // 4
        
        # Step 2: Determine the excess number of each character
        excess = {char: max(0, count[char] - target_count) for char in count}
        
        # If there are no excess characters, the string is already balanced
        if sum(excess.values()) == 0:
            return 0
        
        # Step 3: Use a sliding window approach to find the minimum length of the substring
        min_length = n
        left = 0
        right = 0
        
        # Current count of characters in the window
        window_count = {'Q': 0, 'W': 0, 'E': 0, 'R': 0}
        
        while right < n:
            # Expand the window by adding the current character
            window_count[s[right]] += 1
            right += 1
            
            # Check if the window is valid (contains enough characters to replace)
            while left < right and all(window_count[char] >= excess[char] for char in excess):
                min_length = min(min_length, right - left)
                # Shrink the window by removing the leftmost character
                window_count[s[left]] -= 1
                left += 1
        
        return min_length

# Example usage:
solution = Solution()
print(solution.balancedString("QWER"))  # Output: 0
print(solution.balancedString("QQWE"))  # Output: 1
print(solution.balancedString("QQQW"))  # Output: 2
263