[c++] fully commented with O(N) time complexity and O(1) space
class Solution {\npublic:\n    \n    string repeatLimitedString(string s, int l) {\n\t\tint m[26]; // variable for storing 26 characters\n\t\n        for(char c
class Solution {\npublic:\n    \n    string repeatLimitedString(string s, int l) {\n\t\tint m[26]; // variable for storing 26 characters\n\t\n        for(char c:s)m[c-\'a\']++; // frequncy count;\n        \n        int i=25,j; //i-> last character ie., \'z\' \n        \n        string res=""; //for storing ans \n        \n        while(i>=0){ // iterating to z-a characters \n            int x=l;  // temparary variable for storing limit;\n            \n            while(m[i] && x){ //if character remain in our map && x has value more than 0\n                res+=(\'a\'+i); //appending the current character\n                m[i]--; //reducing the frequency count\n                x--; //reducing the limit as well\n            }\n            \n            if(m[i]){ // current character still has more frequency to come again\n                int j=i-1; //hence pointing the next character which is less than the current character;\n                \n                while(j>=0 && m[j]==0) // after this loop j will be a valid chacter which has frequency;\n                    j--;\n                \n                if(j>=0){ //if valid character exist then \n                    res+=(\'a\'+j); // add that valid character\n                    m[j]--; //and reduce its frequency\n                }\n                else //if j has value less than 0 implies no valid character exist as character can be in range [0, 25]\n                    break;\n            }\n            if(!m[i]) //if that current character has no value then decrement it\n                i--;\n        }\n        return res;\n        \n        // time complexity -> O( 26*N ) == O(N) , where N is the size of string;\n        // space complexity -> O(1) \n    }\n};\n\n**if you find it helpful please upvote**\n**Time complexity -> O( 26*N ) == O(N) , where N is the size of string;\n space complexity -> O(1)**
['C', 'C++']