C++ Solution || 2 Approaches || Detailed Explanation
IntuitionThe key observation is that to make the string lexicographically largest, we need to use the highest possible characters first, as long as we respect t
# Intuition\nThe key observation is that to make the string lexicographically largest, we need to use the highest possible characters first, as long as we respect the `repeatLimit` constraint. Once we hit the `repeatLimit`, we must insert the next lexicographically smaller character before continuing.\n\n# Approach 1: Using Priority Queue\n## Approach\n1. **Count Character Frequencies**: Use a frequency array to count occurrences of each character in the string.\n2. **Priority Queue**: Insert characters into a max priority queue so that the lexicographically largest character is processed first.\n3. **Build the Result**:\n    - Append the current largest character up to `repeatLimit` times.\n    - If further occurrences of the current character remain, temporarily insert the next lexicographically smaller character to "break" the limit.\n    - Push back the remaining characters into the priority queue for future processing.\n4. Repeat until all characters are processed.\n\n# Complexity\n- Time complexity: $$O(n + 26log(26))$$\n    - Counting frequencies takes $$\uD835\uDC42(\uD835\uDC5B)$$ time.\n    - Each push and pop operation in the priority queue costs $$O(log26)$$, and we process at most 26 distinct characters.\n\n- Space complexity: $$O(26)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int repeatLimit) {\n        vector<int> count(26, 0);\n        for (char& ch : s) {\n            count[ch - \'a\']++;\n        }\n\n        priority_queue<char> pq;\n        for (int i = 0; i < 26; i++) {\n            if (count[i] > 0) {\n                pq.push(\'a\' + i);\n            }\n        }\n\n        string res;\n        while (!pq.empty()) {\n            char ch = pq.top();\n            pq.pop();\n            int freq = min(count[ch - \'a\'], repeatLimit);\n            res.append(freq, ch);\n            count[ch - \'a\'] -= freq;\n\n            if (count[ch - \'a\'] > 0 && !pq.empty()) {\n                int nextchar = pq.top();\n                pq.pop();\n                res.push_back(nextchar);\n                count[nextchar - \'a\']--;\n                if (count[nextchar - \'a\'] > 0) {\n                    pq.push(nextchar);\n                }\n\n                if (count[ch - \'a\'] > 0) {\n                    pq.push(ch);\n                }\n            }\n        }\n\n        return res;\n    }\n};\n```\n\n\n# Approach 2: Using Frequency Array and Greedy\n## Approach\n1. **Count Character Frequencies**: Store the frequency of each character using a frequency array.\n2. **Iterate in Reverse**: Traverse from the largest character (\'z\') to the smallest (\'a\').\n3. **Build the Result**:\n    - Add the current character up to `repeatLimit` times.\n    - If further occurrences remain, find the next lexicographically smaller character and append it to "break" the repetition limit.\n4. Repeat the process until all characters are exhausted.\n\n# Complexity\n- Time complexity: $$O(n + 26)$$\n    - Counting frequencies takes $$\uD835\uDC42(\uD835\uDC5B)$$ time.\n    - Processing all 26 characters in reverse order takes $$O(26)$$ time.\n\n- Space complexity: $$O(26)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int repeatLimit) {\n        vector<int> count(26, 0);\n        for(char &ch : s) {\n            count[ch-\'a\']++;\n        }\n\n        string result;\n\n        int i = 25; \n        while(i >= 0) { \n            if(count[i] == 0) {\n                i--;\n                continue;\n            }\n\n            char ch  = \'a\' + i; \n            int freq = min(count[i], repeatLimit);\n\n            result.append(freq, ch);\n            count[i] -= freq;\n\n            if(count[i] > 0) {\n                int j = i-1;\n                while(j >= 0 && count[j] == 0) {\n                    j--;\n                }\n\n                if(j < 0) {\n                    break;\n                }\n\n                result.push_back(\'a\' + j);\n                count[j]--;\n            }\n        }\n\n        return result;\n    }\n};\n\n```\n\n![1721315779911.jpeg](https://assets.leetcode.com/users/images/2775b45f-e161-452e-9170-794c4606db7c_1734437105.5858996.jpeg)\n
['Hash Table', 'String', 'Heap (Priority Queue)', 'Counting', 'C++']