Untitled
unknown
plain_text
2 years ago
653 B
6
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;
priority_queue<pair<int,char>>pq; //freq,element
for(auto i:s){
f[i]++;
}
for(auto i:f){
pq.push({i.second,i.first});
}
string res="";
while(!pq.empty()){
pair<int,char>i =pq.top(); pq.pop();
int count=i.first;
char ch=i.second;
while(count--) res=res+ch;
}
return res;
}
};Editor is loading...
Leave a Comment