C++, Worst to best, Bruteforce (N^2), priorityQueue(O(NlogN)), Counting + greedy(O(N))
Bruteforce Solution\n#1 Bruteforce Approach O(N^2) not accepted (failed at 144th testcase were the limit is \'1\')\n1. first sort the string.\n2. now traverse t
# Bruteforce Solution\n**#1 Bruteforce Approach O(N^2) not accepted (failed at 144th testcase were the limit is \'1\')**\n1. first sort the string.\n2. now traverse through the string and find the index were the *limit* is reached as soon as the limit reached find the next element which is not equal to the current element \n3. then swap the both element \n4. after doing that it can be observe that the resultant string is left with the extra character at the end of the string \n\tEg:\n\tstring s = **"aaaaaaabbbccccd"**\n\tlimit = **3**\n\t* resultant string looks like \n\t\t\t **"dcccbcbbaaaaaaa"**\n\t* now we need to pop some of the last element from the string and we have done\n\t* to do so, we need to count the occurence of the last character (***in example it is \'a\' = 7*** )\n\t* Number of character to be deleted will be = **count - limit** *i.e* **7-3 = 4**\n 5. now just delete the required character from the string and done!.\n\t  \n\t  resultant string  =  **"dcccbcbbaaaaaaa"**\n\t  count = 7, limit = 3;\n\t  noOfdelete = 7 - 3 = 4\n\t  After deletion string = **""dcccbcbbaaa   |   aaaa""**\n\t  \n\t  C++ Code : \n```\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int repeatLimit) {\n        \n        sort(s.rbegin(), s.rend());\n        int count = 0;int j = 0;\n        for(int i=0; i<s.size()-1; i++)\n        {\n            j = i;\n            if(count >= repeatLimit)\n            { \n                for( ; j<s.size(); j++)\n                {\n                    if(s[i] != s[j])\n                    {\n                        swap(s[i], s[j]);   \n                        break;\n                    }\n                }\n                count = 0;\n            }\n            if(s[i] != s[i+1])count=0;\n            else\n            {\n                count++;\n            }\n        }\n        int c = 1;\n        for(int i=s.size()-2; i>=0; i--)\n        {\n            if(s[i] != s[i+1] && i-1 >= 0)\n            {\n                break;\n            }\n            c++;\n        }\n        if(c <= repeatLimit)return s;\n        int t = c - repeatLimit;\n        while(t-- && !s.empty())s.pop_back();\n         \n        return s;\n    }\n};\n\t  \n```\n# Priority Queue Solution (Better)\n**priority queue O(NLogN), accepted**\n\n1. count all occurence of each and every character (*using map or a vector*)\n2. make a priority queue of pair and push all the element and frequency in it.\n3. Push the very first character in the answer minimum of limit and the frequency of the first character in the priority queue *i.e.* **push character X min(limit, characterFreq)**\n4. reduce the characterFreq as **characterFreq -= limit**\n5. if characterFreq is -ve we will continue\n6. else if the priority queue is empty then simply return the anwer \n7. else push one occurence of the top most character in priority queue \n8. also reduce the newFrew by 1 *i.e.* newFreq -= 1\n\nNOTE : You will understood the approach more clear by dry runnig the code \n\nCODE C++:\n```\n\nclass Solution{\n    public:\n    string repeatLimitedString(string s, int limit)\n    {\n        map<char,int> mpp;\n        for(auto i:s)\n        {\n            mpp[i]++;\n        }\n        \n        priority_queue<pair<char,int>> pq;\n        for(auto i:mpp)\n        {\n            pq.push({i.first, i.second});\n        }\n        \n        string ans;\n        while(!pq.empty())\n        {\n            pair<char,int> temp = pq.top(); pq.pop();\n            \n            point : // ( * )\n            \n            int fill = min(limit, temp.second);\n            temp.second -= fill;\n            \n            while(fill--)ans.push_back(temp.first);\n            if(temp.second > 0)\n            {\n                if(pq.empty())return ans;\n                \n                pair<char, int> now = pq.top(); pq.pop();\n                ans.push_back(now.first);\n                now.second -= 1;\n                if(now.second) pq.push(now);\n                goto point; ( * )\n            }\n        }\n        return ans;\n    }\n};\n```\n\n\n**I personally came up with only these two approach, the best Approach actually idea of @lzl124631x Thanks @lzl124631x for giving such optimised solution**\n\nlink to his blog : https://leetcode.com/problems/construct-string-with-repeat-limit/discuss/1784718/C%2B%2B-Greedy-%2B-Counting-O(N)-Time-O(1)-space\n\n\nNOTE : This is my very first blog please forgive any mistake done in the blog.\n
['Greedy', 'C', 'Heap (Priority Queue)', 'Counting Sort', 'C++']