Clear 19ms C++, Sorting vector<pair<>>,


  • 0
    Y
    class Solution {
    public:
        string frequencySort(string s) {
            string res;
            if (s.size()==0) return res;
            vector<pair<int,int>> map(256,make_pair(0,0));//<freq, char-char(0)>
            for (int i = 0; i<256; i++) {
                map[i].second = i;
            }
            for(int i = 0; i < s.size(); i++)
                map[s[i]].first++;
            sort(map.begin(), map.end(), greater<pair<int,int>>());// sort pairs by freq
            int i = 0;
            while(map[i].first != 0) {
                for (int j = 0; j < map[i].first;j++)
                    res.push_back(char(0)+map[i].second);
                i++;
            }
            return res;
        }
    };
    

Log in to reply
 

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