package leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * https://leetcode.com/problems/increasing-decreasing-string/
 */
public class Problem1370 {
    public String sortString(String s) {
        TreeMap<Character, Integer> map = new TreeMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
        }
        String answer = "";
        boolean asc = true;
        while (map.size() > 0) {
            List<Character> keys = asc ?
                new ArrayList<>(map.keySet()) : new ArrayList<>(map.descendingKeySet());
            for (char key : keys) {
                answer += key;
                int c = map.get(key) - 1;
                if (c == 0) {
                    map.remove(key);
                } else {
                    map.put(key, c);
                }
            }
            asc = !asc;
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1370 prob = new Problem1370();
        System.out.println(prob.sortString("aaaabbbbcccc")); // "abccbaabccba"
        System.out.println(prob.sortString("rat")); // "art"
        System.out.println(prob.sortString("leetcode")); // "cdelotee"
        System.out.println(prob.sortString("ggggggg")); // "ggggggg"
        System.out.println(prob.sortString("ggggggg")); // "ggggggg"
        System.out.println(prob.sortString("spo")); // "ops"
    }
}

@@ -972,515 +972,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1370 prob = new Problem1370();%0A        System.out.println(prob.sortString(%22aaaabbbbcccc%22)); // %22abccbaabccba%22%0A        System.out.println(prob.sortString(%22rat%22)); // %22art%22%0A        System.out.println(prob.sortString(%22leetcode%22)); // %22cdelotee%22%0A        System.out.println(prob.sortString(%22ggggggg%22)); // %22ggggggg%22%0A        System.out.println(prob.sortString(%22ggggggg%22)); // %22ggggggg%22%0A        System.out.println(prob.sortString(%22spo%22)); // %22ops%22%0A    %7D
 %0A%7D%0A

Solve problem 1370