C++ clean and easy solution by using map and multimap


  • 0
    L

    class Solution {
    public:
    string frequencySort(string s) {
    map<char,int> p;
    for(char c:s){
    p[c]++;
    }
    typedef multimap<int,char,greater<int>> MID;
    MID p2;
    for(map<char,int>::iterator it = p.begin(); it != p.end(); it++){
    p2.insert(MID::value_type(it->second,it->first));
    }
    string res;
    for(multimap<int,char>::iterator it = p2.begin(); it !=p2.end(); it++){
    for(int n=0; n< it->first; n++){
    res.insert(res.end(),it->second);
    }
    }
    return res;
    }
    };


Log in to reply
 

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