C++ O(1) space, O(N) time | explanation included
IntuitionWe need to construct a string that is lexicographically the largest and not have characters repeat more then a limit times in a row.ApproachWe will sta
# Intuition\nWe need to construct a string that is lexicographically the largest and not have characters repeat more then a limit times in a row.\n\n# Approach\nWe will start with chracter \'z\' and make our way down the alphabet to ensure we arrive at the lexicographically largest string. \nWe first map out how many characters we have to use by iterating over th original string and keeping count of the appearences of the characters.\nWe now have a varible i and tmp for the current largest character we will be using and a backup character for when we reach the repeating limit.\nWe also create a bool \'nomore\' to check if besides our current character (i) is ther another one. Or another way to view it is if there is another characters smaller then i. This is to check if we can still continue after repeating limit amoutn of times.\nSet tmp by going down from 24 (skip 25 for i) to find the first place where cha[tmp] != 0 (means we have a letter we can use)\nWe then while loop i >= 0 then we will try to repeat limit amount of times on our current letter.\n- If we reach the limit then if there is \'nomore\' letters smaller we break (as there is no way to continue and ensure the problems conditions)\n- Else if we ran out of letters at our current position we then set i to tmp and find a new tmp\n- Else if we ran into the limit of repeating letters we then add tmp\'s charater and continue.\n**Make sure to check if tmp\'s character runs out after we add it into our ans string to look for a new one\n\n# Complexity\n- Time complexity:O(N)\n\n- Space complexity:O(1)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int repeatLimit) {\n        vector<int> cha(26, 0);\n        for(int i = 0; i < s.length(); i++){\n            cha[s[i]-\'a\']++;\n        }\n        int i = 25, tmp = i-1;\n        bool nomore = false;\n        while(cha[tmp] == 0){\n            tmp--;\n            if(tmp < 0){\n                nomore = true;\n                break;\n            }\n        }\n        string ans;\n        while(i >= 0){\n            int k = 0;\n            for(; k < repeatLimit; k++){\n                if(cha[i] <= 0) break;\n                ans += (\'a\'+i);\n                cha[i]--;\n            }\n            if(nomore) break;\n            if(cha[i] <= 0){\n                i = tmp; tmp--;\n                if(tmp < 0){\n                    nomore = true;\n                    continue;\n                }\n                while(cha[tmp] == 0 && !nomore){\n                    tmp--;\n                    if(tmp < 0){\n                        nomore = true;\n                        break;\n                    }\n                }\n            }else if(k == repeatLimit){\n                ans += (\'a\' + tmp);\n                cha[tmp]--;\n                while(cha[tmp] == 0 && !nomore){\n                    tmp--;\n                    if(tmp < 0){\n                        nomore = true;\n                        break;\n                    }\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']