Simple Cpp solution without using Sort() that beats 96.4 percent


  • 0
    S
    class Solution {
    public:
    string frequencySort(string s) {
        
        int counter[256] = {0};
        multimap <int,char, std::greater<int>> m_counter;
        string result;
        
        for (char c: s)
        {
            counter[(int)c]++;
            
        }
        
        for (int i =0 ; i<256; i++)
        {
            
            if (counter[i] > 0)
            {
                m_counter.insert(pair<int,char>(counter[i],(char)i));
            }
            
        }
        
        
        for (auto cn:m_counter)
        {
            result.append(cn.first,cn.second);
        }
        
        return result;
        
        
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.