-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY 17
More file actions
33 lines (24 loc) · 773 Bytes
/
DAY 17
File metadata and controls
33 lines (24 loc) · 773 Bytes
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
//SORT CHAR BY FREQUENCY
class Solution {
public:
string frequencySort(string s) {
auto cmp = [](const pair<char, int>& a, const pair<char, int>& b) {
return a.second < b.second;
};
priority_queue<pair<char, int>, vector<pair<char, int>>, decltype(cmp)> pq(cmp);
unordered_map<char, int> hm;
for (char c : s) {
hm[c]++;
}
for (const auto& entry : hm) {
pq.push(make_pair(entry.first, entry.second));
}
string result = "";
while (!pq.empty()) {
pair<char, int> p = pq.top();
pq.pop();
result.append(p.second, p.first);
}
return result;
}
};