0%

Leetcode451-sortCharacterByFrequency

Description

Given a string, sort it in decreasing order based on the frequency of characters.

Example

Example 1:

1
2
3
4
5
6
7
8
9
Input:
"tree"

Output:
"eert"

Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.

Example 2:
1
2
3
4
5
6
7
8
9
Input:
"cccaaa"

Output:
"cccaaa"

Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.

Example 3:
1
2
3
4
5
6
7
8
9
Input:
"Aabb"

Output:
"bbAa"

Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.

Solution

Note: In Problems which need to use Hash Table to store single letters/digits, using int[] map = new int[128] instead of HashMap could speed up lots of time.

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
class Solution {
public String frequencySort(String s) {
if (s == null || s.length() <= 1) return s;

int[] map = new int[128];
PriorityQueue<Character> pq = new PriorityQueue<>(new Comparator<Character>(){
@Override
public int compare(Character a, Character b){
return map[b] - map[a];
}
});

for (char ch: s.toCharArray()){
map[ch]++;
}
for (int i = 0; i < 128; i++)
if (map[i] > 0)
pq.offer((char)i);

StringBuilder res = new StringBuilder();
while (!pq.isEmpty()){
char tmp = pq.poll();
for (int i = 0; i < map[tmp]; i++) res.append(tmp);
}
return res.toString();
}
}