O(nlogn) using C++ beats 81.76%


  • 0
    N
    struct storeSt{
      char c;
      int cnt;
      bool operator < (const storeSt& rhs) const { return cnt > rhs.cnt; }
    };
    class Solution {
    public:
        string frequencySort(string s) {
            storeSt t[200];
            string ss = "";
            for(int i=0;i<200;i++)
                t[i].cnt=0;
            for(int i=0;i<s.length();i++){
                t[s[i]].c = s[i];
                t[s[i]].cnt++;
            }
            sort(t,t+200);
            int i=0;
            while(t[i].cnt!=0){
                for(int j=0;j<t[i].cnt;j++)
                    ss+=t[i].c;
                i++;
            }
            return ss;
        }
    };
    

Log in to reply
 

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