Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
634 B
1
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;
        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;
    }
};
Leave a Comment