12ms C++ solution

  • 0
        static bool myComp(pair<char,int> a, pair<char,int> b)
            return a.second > b.second;
        string frequencySort(string s) {
            vector<pair<char,int>> freq(256, pair<char,int>());
            for(char c : s)
                freq[c].first = c;
            sort(freq.begin(), freq.end(), myComp);
            string res;
            for(pair<char, int> pa : freq)
                res += string(pa.second, pa.first);
            return res;

  • 0

    Why not lambda expression?

Log in to reply

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