-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
50 lines (45 loc) · 1.55 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
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
}
public String frequencySort_pq(String s) {
Map<Character, Integer> map = new HashMap<>();
for (Character c : s.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1);
PriorityQueue<Character> heap = new PriorityQueue<>((a, b) -> map.get(b) - map.get(a));
for (Character c : map.keySet())
heap.offer(c);
StringBuilder res = new StringBuilder();
while (!heap.isEmpty()) {
Character c = heap.poll();
for (int i = 0; i < map.get(c); i++)
res.append(c);
}
return res.toString();
}
public String frequencySort(String s) {
Map<Character, Integer> map = new HashMap<>();
int max = 0;
for (Character c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
max = Math.max(max, map.get(c));
}
List<Character>[] freq = new List[max + 1];
for (Character c : map.keySet()){
if (freq[map.get(c)] == null)
freq[map.get(c)] = new ArrayList<>();
freq[map.get(c)].add(c);
}
StringBuilder res = new StringBuilder();
for (int i = max; i >= 0; i--) {
if (freq[i] == null) continue;
for (Character c : freq[i]){
for (int j = 0; j < i; j++)
res.append(c);
}
}
return res.toString();
}
}