 ✅ | Beats 95% | O(N * 26) time | O(26) Space | Greedy Strategy
IntuitionFor Making a string Lexicographically largest, we will follow a Greedy Strategy
For making string lexicographically largest, always put largest alphabe
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFor Making a string Lexicographically largest, we will follow a **Greedy Strategy**\n> For making string lexicographically largest, always put largest alphabet in the begining (if possible), otherwise, find the next largest alphabet. \n\n---\n\n\nNow the case will come when number alphabets will exceed the repeatLimit. In that case, we will look for **next largest element** available, **take its only one occurance**, and then again continue with the largest element. \n> Ex.\n*s = "aaabbbccc*\n*repeatLimit = 2*\n*Here, first select 2 **c** --> cc*\n*Then look for next largest element, **b**. Since all **c** are not used yet, take only one **b** -> ccb*\n*Now, again take **c** --> ccbc*\n*Since **c** is exhausted, largest alphabet now is **b** --> ccbcbb*\n*Now **b** is also exhasted, take **a** --> ccbcbbaa*\n*We can only take 2 **a** otherwise it will exceed repeatLimit*\n\nWe are also making sure, if some alphabets are wasted, (in above example **a**), it should be the smallest alphabet. \n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Store frequency all the alphabets. \n- Greedily pick the largest alphabet, and take **atmost** **repeatLimit** occrances of it. Append it to answer string. \n- Now if all occurances are exhausted, simply move to next largest alphabet and repeat the process. Otherwise, take only one occurance of next largest alphabet and append it into the answer, and repeat step 2. \n- If next largest element does not exist, end the process as no more alphabets can be appended. \n\n# Complexity\n- Time complexity: O(N * 26)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(26)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string repeatLimitedString(string &s, int &repeatLimit) {\n        int h[26] = {}; \n        for (auto &ch: s) {\n            h[ch - \'a\']++; \n        }\n        string ans; \n        for (int i = 25; i >= 0; i--) {\n            while (h[i] > 0) {\n                int x = min(repeatLimit, h[i]); \n                h[i] -= x; \n                while (x--) {\n                    ans += (i + \'a\'); \n                }\n                if (h[i] == 0) {\n                    break; \n                }\n                bool flag = false; \n                for (int j = i - 1; j >= 0; j--) {\n                    if (h[j]) {\n                        ans += (j + \'a\'); \n                        h[j]--; \n                        flag = true; \n                        break; \n                    }\n                }\n                if (!flag) {\n                    break; \n                }\n            }\n        }\n        return ans; \n    }\n};\n```
['Hash Table', 'String', 'Greedy', 'Counting', 'C++']