# 1234. Replace the Substring for Balanced String

[Link](https://leetcode.com/problems/replace-the-substring-for-balanced-string/)

**Solution**

這題也是slidewindow的標準題型，先移動end找到符合條件的，再移動start去找到最小的

```java
    public int balancedString(String s) {
        int ideaCount = s.length() / 4;
        
        int[] counts = new int[128];
        for(int i = 0; i < s.length(); i++){
          counts[s.charAt(i)]++;
        }
        int start = 0, end = 0;
        int len = s.length();
        while(end < s.length()){
           counts[s.charAt(end)]--;
           while(start < s.length() && 
                 counts['Q'] <= ideaCount &&
                counts['W'] <= ideaCount &&
                counts['E'] <= ideaCount &&
                counts['R'] <= ideaCount ){
               
               len = Math.min(len, end-start+1);
               counts[s.charAt(start)]++;
               start++;
           }
           end++;
        }
        return len;
        
    }
}
```
