1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class Solution { public String rankTeams(String[] votes) { Map<Character, int[]> map = new HashMap(); int l = votes[0].length(); for (String v : votes) { for (int i = 0; i < l; ++i) { map.putIfAbsent(v.charAt(i), new int[l]); map.get(v.charAt(i))[i]++; } } List<Character> list = new ArrayList<>(map.keySet()); Collections.sort(list, (a, b) -> { for (int i = 0; i < l; ++i) { if (map.get(a)[i] != map.get(b)[i]) { return map.get(b)[i] - map.get(a)[i]; } } return a - b; }); StringBuilder sb = new StringBuilder(); for (char c : list) { sb.append(c); } return sb.toString(); } }
|