1class Solution {2public:3string frequencySort(string s) {4unordered_map<char, int> mp;5for (int i = 0; i < s.length(); i++) // get the frequency of every char of the string6{7mp[s[i]]++;8}910priority_queue<pair<int, char>> pq; // store the freq and char pair in max heap11for (auto it = mp.begin(); it != mp.end(); it++) {12pq.push({it->second, it->first});13}14string str = "";15int val;16while (!pq.empty()) {17val = pq.top().first; // append the char frequency times18while (val--) {19str.push_back(pq.top().second);20}21pq.pop();22}23return str;24}25};