package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * https://leetcode.com/problems/rank-teams-by-votes/
 */
public class Problem1366 {
    public String rankTeams(String[] votes) {
        Map<Character, TreeMap</* rank */ Integer, /* count */ Integer>> map = new HashMap<>();
        for (String vote : votes) {
            for (int i = 0; i < vote.length(); i++) {
                if (!map.containsKey(vote.charAt(i))) {
                    map.put(vote.charAt(i), new TreeMap<>());
                }
                map.get(vote.charAt(i)).put(i, map.get(vote.charAt(i)).getOrDefault(i, 0) + 1);
            }
        }
        List<Character> chars = new ArrayList<>(map.keySet());
        Collections.sort(chars, (a, b) -> {
            List<Map.Entry<Integer, Integer>> list1 = new ArrayList<>(map.get(a).entrySet());
            List<Map.Entry<Integer, Integer>> list2 = new ArrayList<>(map.get(b).entrySet());
            int size = Math.max(list1.size(), list2.size());
            int i = 0;
            while (i < size) {
                Map.Entry<Integer, Integer> entry1 = list1.get(i);
                Map.Entry<Integer, Integer> entry2 = list2.get(i);
                // Compare rank.
                int cmp = Integer.compare(entry1.getKey(), entry2.getKey());
                if (cmp != 0) {
                    return cmp;
                }
                // Compare number of votes.
                cmp = Integer.compare(entry2.getValue(), entry1.getValue());
                if (cmp != 0) {
                    return cmp;
                }
                i++;
            }
            // Compare by character.
            return Character.compare(a, b);
        });
        String answer = "";
        for (char c : chars) {
            answer += c;
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1366 prob = new Problem1366();
        System.out.println(prob.rankTeams(new String[]{"ABC","ACB","ABC","ACB","ACB"})); // "ABC"
        System.out.println(prob.rankTeams(new String[]{"WXYZ","XYZW"})); // "XWYZ"
        System.out.println(prob.rankTeams(new String[]{"ABC","CBA"})); // "ABC"
        System.out.println(prob.rankTeams(new String[]{"ABDC","CDBA"})); // "ACBD"
        System.out.println(prob.rankTeams(new String[]{"CBDA","ADBC"})); // "ACBD"
        System.out.println(prob.rankTeams(new String[]{"WYXZ","XYZW"})); // "XWYZ"
        System.out.println(prob.rankTeams(new String[]{"ZMNAGUEDSJYLBOPHRQICWFXTVK"})); // "ZMNAGUEDSJYLBOPHRQICWFXTVK"
        System.out.println(prob.rankTeams(new String[]{"BCA","CAB","CBA","ABC","ACB","BAC"})); // "ABC"
        System.out.println(prob.rankTeams(new String[]{"M","M","M","M"})); // "M"
    }
}

@@ -92,24 +92,51 @@
 il.HashMap;%0A
+import java.util.Iterator;%0A
 import java.
@@ -142,24 +142,24 @@
 .util.List;%0A
-
 import java.
@@ -883,36 +883,40 @@
 %3E %7B%0A            
-List
+Iterator
 %3CMap.Entry%3CInteg
@@ -933,32 +933,16 @@
 r%3E%3E 
-list1 = new ArrayList%3C%3E(
+iter1 = 
 map.
@@ -950,32 +950,42 @@
 et(a).entrySet()
+.iterator(
 );%0A            L
@@ -983,20 +983,24 @@
         
-List
+Iterator
 %3CMap.Ent
@@ -1025,32 +1025,16 @@
 r%3E%3E 
-list2 = new ArrayList%3C%3E(
+iter2 = 
 map.
@@ -1046,24 +1046,34 @@
 ).entrySet()
+.iterator(
 );%0A         
@@ -1079,107 +1079,49 @@
     
-int size = Math.max(list1.size(), list2.size());%0A            int i = 0;%0A            while (i %3C size
+while (iter1.hasNext() && iter2.hasNext()
 ) %7B%0A
@@ -1177,19 +1177,19 @@
 1 = 
-list1.ge
+iter1.nex
 t(
-i
 );%0A 
@@ -1244,19 +1244,19 @@
 2 = 
-list2.ge
+iter2.nex
 t(
-i
 );%0A 
@@ -1653,29 +1653,8 @@
   %7D%0A
-                i++;%0A
     
@@ -1842,32 +1842,32 @@
 += c;%0A        %7D%0A
+
         return a
@@ -1882,921 +1882,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1366 prob = new Problem1366();%0A        System.out.println(prob.rankTeams(new String%5B%5D%7B%22ABC%22,%22ACB%22,%22ABC%22,%22ACB%22,%22ACB%22%7D)); // %22ABC%22%0A        System.out.println(prob.rankTeams(new String%5B%5D%7B%22WXYZ%22,%22XYZW%22%7D)); // %22XWYZ%22%0A        System.out.println(prob.rankTeams(new String%5B%5D%7B%22ABC%22,%22CBA%22%7D)); // %22ABC%22%0A        System.out.println(prob.rankTeams(new String%5B%5D%7B%22ABDC%22,%22CDBA%22%7D)); // %22ACBD%22%0A        System.out.println(prob.rankTeams(new String%5B%5D%7B%22CBDA%22,%22ADBC%22%7D)); // %22ACBD%22%0A        System.out.println(prob.rankTeams(new String%5B%5D%7B%22WYXZ%22,%22XYZW%22%7D)); // %22XWYZ%22%0A        System.out.println(prob.rankTeams(new String%5B%5D%7B%22ZMNAGUEDSJYLBOPHRQICWFXTVK%22%7D)); // %22ZMNAGUEDSJYLBOPHRQICWFXTVK%22%0A        System.out.println(prob.rankTeams(new String%5B%5D%7B%22BCA%22,%22CAB%22,%22CBA%22,%22ABC%22,%22ACB%22,%22BAC%22%7D)); // %22ABC%22%0A        System.out.println(prob.rankTeams(new String%5B%5D%7B%22M%22,%22M%22,%22M%22,%22M%22%7D)); // %22M%22%0A    %7D
 %0A%7D%0A

Solve problem 1366