Simple C++ solution with multimap


  • 0
    L
    string frequencySort(string s) {
        map<char, int> f;
        for(auto& c: s) f[c]++;
    
        multimap<int, char> mm;
        for(auto&m : f) mm.insert(make_pair(m.second, m.first));
    
        string ret;
        for(auto it = mm.rbegin(); it != mm.rend(); it++)
            for(int i = 0; i < it->first; i++) ret.push_back(it->second);
        
        return ret;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.