-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
28 lines (28 loc) · 899 Bytes
/
Solution.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
class Solution {
public String[] sortFeatures(String[] features, String[] responses) {
Map<String, Integer> cnt = new HashMap<>();
for (String r : responses) {
Set<String> ws = new HashSet<>();
for (String w : r.split(" ")) {
ws.add(w);
}
for (String w : ws) {
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
}
}
int n = features.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
Arrays.sort(idx, (i, j) -> {
int d = cnt.getOrDefault(features[j], 0) - cnt.getOrDefault(features[i], 0);
return d == 0 ? i - j : d;
});
String[] ans = new String[n];
for (int i = 0; i < n; ++i) {
ans[i] = features[idx[i]];
}
return ans;
}
}