Untitled

 avatar
NguyenAnhQuan
c_cpp
22 days ago
773 B
0
Indexable
Never
class Solution {
public:
    
    int hash_function(char value)
    {
        return int(value);
    }

    string frequencySort(string s) {
        
        stringstream ss;

        vector <int> v(130, 0);

        for (int i = 0; i < int(s.length()); i++)
        {
            int hash = hash_function(s[i]);
            v[hash]++;
        }

        vector <pair<int, int>> vs;

        for (int i = 0; i < 128; i++)
            if (v[i] != 0) vs.push_back({v[i], i});

        sort(vs.begin(), vs.end(), greater <pair<int, int>>());

        for (int i = 0; i < int(vs.size()); i++)
        {
            for (int j = 1; j <= vs[i].first; j++)
                ss << char(vs[i].second);
        }

        return ss.str();
    }

};
Leave a Comment