题目:
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
class Solution {
public:
string frequencySort(string s) {
string t;
int len = s.size();
map<char,int> re;
for(int i = 0; i < len; i++){
re[s[i]]++;
}
while(1){
int flag = -1;
char max ;
for(auto val:re){
if(val.second>flag) max = val.first,flag = val.second;
}
if(flag == -1) return t;
while(flag--){
t+=max;
}
re[max] = -1;
}
}
};