-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path451.cpp
More file actions
34 lines (27 loc) · 816 Bytes
/
Copy path451.cpp
File metadata and controls
34 lines (27 loc) · 816 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
32
33
#include <functional>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
class Solution {
public:
string frequencySort(string s) {
unordered_map<char, int> map;
vector<pair<char,int>> vec;
string res;
for(char ch : s) map[ch]++;
// for(char ch : s) {
// if (map.find(ch) == map.end()) {
// map.insert({ch, 1});
// } else {
// map[ch]++;
// }
// }
for (auto p : map) vec.push_back(p);
// sort(vec.begin(), vec.end(), greater<int>());
sort(vec.begin(), vec.end(), [](auto a, auto b) { return a.second > b.second; });
for (auto i : vec) res += string(i.second, i.first);
return res;
}
};