Untitled
unknown
plain_text
2 years ago
634 B
10
Indexable
class Solution {
public:
static bool cmp(pair<int,int>&a, pair<int,int>&b){
return a.second >b.second;
}
string frequencySort(string s) {
unordered_map<char,int>f;
vector<pair<int,int>>freq; // char,frequency
for(auto i:s){
f[i]++;
}
for(auto i:f){
freq.push_back({i.first,i.second});
}
sort(freq.begin(),freq.end(),cmp);
string res="";
for(auto i:freq){
int count=i.second;
char ch=i.first;
while(count--) res=res+ch;
}
return res;
}
};Editor is loading...
Leave a Comment