Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
443 B
1
Indexable
Never
class Solution {
public:
    string frequencySort(string s) {
        unordered_map<char, int> f;
        for (char c : s) {
            f[c]++;
        }

        multiset<pair<int, char>, greater<>> freqSet;
        for (auto& [ch, freq] : f) {
            freqSet.insert({freq, ch});
        }

        string res = "";
        for (auto& [freq, ch] : freqSet) {
            res += string(freq, ch);
        }

        return res;
    }
};
Leave a Comment