Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
653 B
0
Indexable
Never
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;
    }
};
Leave a Comment