C++ solution O(nlogn)
\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int rl) {\n        int limit = rl;\n        int n= s.size();\n        sort(s.begin(), s.en
```\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int rl) {\n        int limit = rl;\n        int n= s.size();\n        sort(s.begin(), s.end());\n        reverse(s.begin(), s.end());\n        //construction of p\n        string p; \n        p.push_back(s[0]);\n        int size = 1;\n        unordered_map<char, int> map;\n        for(int i = 0; i<n; i++){\n            map[s[i]]++;\n            if(s[i]!= p[size-1]){\n                size++;\n                p.push_back(s[i]);\n            }\n        }\n        //for(int i = 0; i<size; i++) cout<<p[i]<<" - "<<map[p[i]]<<endl;\n        \n        string res;\n        if(size == 1){\n            for(int i = 0; i< min(limit, map[p[0]]); i++) res.push_back(p[0]);\n            return res;\n        }\n        int curr = 0;\n        int next = 1;\n        while(next < size){\n            if(!map[p[curr]]) {\n                curr = next;\n                next++;\n                continue;\n            }\n            //limit >= fre\n            if(limit >= map[p[curr]] and map[p[curr]]){\n                //push those\n                for(int i=0; i<map[p[curr]]; i++) res.push_back(p[curr]);\n                map[p[curr]] = 0;\n                curr = next;\n                next++;\n                //limit = rl; //reseting it to rl\n                continue;\n            }\n            //limit < fre\n            for(int i = 0; i<limit; i++){\n                //push limit times\n                res.push_back(p[curr]);\n            }\n            map[p[curr]]-= limit;\n            if(map[p[next]]){\n                res.push_back(p[next]);\n                map[p[next]]--;\n            }\n            else {\n                next++;\n                if(next == size) break;\n                res.push_back(p[next]);\n                map[p[next]]--;\n            }\n        }\n        if(curr<size and map[p[curr]] and p[curr] != res[res.size()-1]){\n            for(int i = 0; i< min(limit, map[p[curr]]); i++) res.push_back(p[curr]);\n        }\n        return res;\n    }\n};\n```
['C']