package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/replace-the-substring-for-balanced-string/
 */
public class Problem1234 {
    public int balancedString(String s) {
        Map<Character, Integer> count = new HashMap<>();
        count.put('Q', 0);
        count.put('W', 0);
        count.put('E', 0);
        count.put('R', 0);
        char[] arr = s.toCharArray();
        for (char c: arr) {
            count.put(c, count.get(c) + 1);
        }
        int need = arr.length / 4;
        int left = 0;
        int right = 0;
        int min = arr.length;
        while (right <= arr.length) {
            if (count.get('Q') > need || count.get('W') > need ||
                count.get('E') > need || count.get('R') > need) {
                if (right >= arr.length) {
                    break;
                }
                count.put(arr[right], count.get(arr[right]) - 1);
                right++;
                continue;
            }
            min = Math.min(min, right-left);
            if (min == 0) {
                break;
            }
            count.put(arr[left], count.get(arr[left]) + 1);
            left++;
        }
        return min;
    }

    public static void main(String[] args) throws Exception {
        Problem1234 prob = new Problem1234();
//        System.out.println(prob.balancedString("QWER")); // 0
//        System.out.println(prob.balancedString("QQWE")); // 1
//        System.out.println(prob.balancedString("QQQW")); // 2
//        System.out.println(prob.balancedString("QQQQ")); // 3
//        System.out.println(prob.balancedString("WQWRQQQW")); // 3
        System.out.println(prob.balancedString("WWEQERQWQWWRWWERQWEQ")); // 4
//        System.out.println(prob.balancedString("QQQWEEER")); // 3
    }
}

@@ -436,16 +436,17 @@
  (char c
+ 
 : arr) %7B
@@ -610,16 +610,29 @@
 length;%0A
+        out:%0A
         
@@ -673,18 +673,21 @@
         
-if
+while
  (count.
@@ -856,32 +856,36 @@
            break
+ out
 ;%0A              
@@ -983,34 +983,8 @@
 ++;%0A
-                continue;%0A
     
@@ -1030,17 +1030,19 @@
 n, right
--
+ - 
 left);%0A 
@@ -1224,592 +1224,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) throws Exception %7B%0A        Problem1234 prob = new Problem1234();%0A//        System.out.println(prob.balancedString(%22QWER%22)); // 0%0A//        System.out.println(prob.balancedString(%22QQWE%22)); // 1%0A//        System.out.println(prob.balancedString(%22QQQW%22)); // 2%0A//        System.out.println(prob.balancedString(%22QQQQ%22)); // 3%0A//        System.out.println(prob.balancedString(%22WQWRQQQW%22)); // 3%0A        System.out.println(prob.balancedString(%22WWEQERQWQWWRWWERQWEQ%22)); // 4%0A//        System.out.println(prob.balancedString(%22QQQWEEER%22)); // 3%0A    %7D
 %0A%7D%0A

Solve problem 1234