-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path451.cpp
53 lines (49 loc) · 1.27 KB
/
451.cpp
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
51
52
53
class Solution
{
public:
string frequencySort(string s)
{
unordered_map<char, int> m1;
// default max and sort by first
priority_queue<pair<int, char>> q1;
for (int i = 0; i < s.length(); i++)
{
m1[s[i]]++;
}
for (auto it = m1.begin(); it != m1.end(); it++)
{
q1.push(make_pair(it->second, it->first));
}
string res = "";
while (q1.size())
{
pair<int, char> y1 = q1.top();
for (int i = 0; i < q1.top().first; i++)
res += q1.top().second;
q1.pop();
}
return res;
}
};
class Solution {
public:
string frequencySort(string s) {
unordered_map<char,int> freq;
vector<string> bucket(s.size()+1, "");
string res;
//count frequency of each character
for(char c:s) freq[c]++;
//put character into frequency bucket
for(auto& it:freq) {
int n = it.second;
char c = it.first;
bucket[n].append(n, c);
}
//form descending sorted string
for(int i=s.size(); i>0; i--) {
if(!bucket[i].empty())
res.append(bucket[i]);
}
return res;
}
};