-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
37 lines (35 loc) · 1.1 KB
/
Main.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
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
public class SortCharactersByFrequency451 {
class Solution {
public String frequencySort(String s) {
HashMap<Character, Item> map = new HashMap<>();
for (char c : s.toCharArray()) {
Item item = map.getOrDefault(c, new Item());
item.freq++;
item.c = c;
map.put(c, item);
}
Item items[] = new Item[map.size()];
int i = 0;
for (Map.Entry<Character, Item> entry : map.entrySet()) {
items[i++] = entry.getValue();
}
Arrays.sort(items, Comparator.comparingInt(x -> -x.freq));
StringBuilder sb = new StringBuilder("");
for (Item item : items) {
while (item.freq > 0) {
sb.append(item.c);
item.freq--;
}
}
return sb.toString();
}
class Item {
char c;
int freq = 0;
}
}
}