Sort Characters By Frequency

2017/12/11 posted in  leetcode

class Solution {
public:
    string frequencySort(string s) {
        unordered_map<char, int> map;
        for (int i =0; i<s.length(); i++) {
            map[s[i]]++;
        }

        sort(s.begin(), s.end(), [&map](char a ,char b){
            return map[a]>map[b] || (map[a] == map[b] && a > b);
        });
        
        return s;
    }
};