forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1366.java
46 lines (41 loc) · 1.32 KB
/
_1366.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.Comparator;
public class _1366 {
public static class Solution1 {
class Node {
int[] count = new int[26];
char c;
public Node(char c) {
this.c = c;
}
}
public String rankTeams(String[] votes) {
Node[] nodes = new Node[26];
for (int i = 0; i < 26; i++) {
nodes[i] = new Node((char) (i + 'A'));
}
for (String vote : votes) {
for (int i = 0; i < vote.length(); i++) {
nodes[vote.charAt(i) - 'A'].count[i]++;
}
}
Arrays.sort(nodes, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
for (int i = 0; i < 26; i++) {
if (o1.count[i] != o2.count[i]) {
return o2.count[i] - o1.count[i];
}
}
return o1.c - o2.c;
}
});
StringBuilder sb = new StringBuilder();
for (int i = 0; i < votes[0].length(); i++) {
sb.append(nodes[i].c);
}
return sb.toString();
}
}
}