-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0451.cpp
More file actions
30 lines (30 loc) · 793 Bytes
/
0451.cpp
File metadata and controls
30 lines (30 loc) · 793 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
class Solution {
public:
string frequencySort(string s) {
if(s.size()==0) return "";
unordered_map<int, int> m;
for (auto c: s) {
m[c]++;
}
sort(s.begin(), s.end(), [&](char a, char b){return m[a] > m[b] || (m[a]==m[b]&&a>b);});
return s;
}
// string frequencySort(string s) {
// if(s.size()==0) return "";
// unordered_map<char, int> freq;
// for (auto c: s) {
// freq[c]++;
// }
// map<int, string> m;
// for(auto p: freq) {
// char ch = p.first;
// int num = p.second;
// m[num] += string(num, ch);
// }
// string res;
// for(auto it=m.rbegin();it!=m.rend();it++) {
// res += it->second;
// }
// return res;
// }
};