class Solution { public: string frequencySort(string s) { map<char,int> m1; multimap<int,char> m2; map<char,int> :: iterator it; multimap<int,char>::iterator it1; string str = ""; for(int i=0;i<s.length();++i) m1[s[i]]++; for(it=m1.begin();it!=m1.end();++it) m2.insert({it->second,it->first}); for(it1=m2.begin();it1!=m2.end();++it1) { for(int j=it1->first;j>0;--j) {str.push_back(it1->second);} } reverse(str.begin(),str.end()); return str; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter