Easy Solution in C++.
Intuitionthe problem of constructing the lexicographically largest string from the characters of s while adhering to the repeatLimit, we can use a greedy approa
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nthe problem of constructing the lexicographically largest string from the characters of s while adhering to the repeatLimit, we can use a greedy approach combined with a max-heap (priority queue). The idea is to always try to add the largest available character to the result string, while ensuring that we do not exceed the repeatLimit for consecutive characters.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Count Character Frequencies: First, we need to count the frequency of each character in the string s. This can be done using an array of size 26 (for each letter of the alphabet).\n\n2. Use a Max-Heap: We will use a max-heap to store the characters based on their lexicographical order. The heap will allow us to efficiently retrieve the largest character available.\n\n3. Construct the Result String:\n\nWhile there are characters left in the heap, we will try to add the largest character to the result string.\nIf the largest character can be added without exceeding the repeatLimit, we add it directly.\nIf adding it would exceed the repeatLimit, we add it as many times as allowed (up to repeatLimit), and then we need to add the next largest character (if available) to break the sequence.\nAfter adding the next character, we can then add the remaining instances of the original character back to the heap if any are left.\n4. Return the Result: After constructing the string, we return it.\n\n## Explanation of the Code:\n1. Frequency Count: We count the occurrences of each character in the input string s.\n\n2. Max-Heap Initialization: We populate a max-heap with characters and their frequencies, ensuring that the largest characters are prioritized.\n\n3. Building the Result: We construct the result string by repeatedly adding the largest character while respecting the repeatLimit. If we need to break a sequence of the same character, we temporarily use the next largest character.\n\n4. Final Output: The constructed string is returned as the result.\n\n# Complexity\n- Time Complexity: The time complexity is (O(n + k \\log k)), where (n) is the length of the string and (k) is the number of unique characters (at most 26). The (O(n)) is for counting frequencies, and (O(k \\log k)) is for managing the heap operations.\n\n- Space Complexity: The space complexity is (O(1\n\n\n\n\n\n\n# Code\n```cpp []\n#include <iostream>\n#include <string>\n#include <vector>\n#include <queue>\n\nusing namespace std;\n\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int repeatLimit) {\n        // Step 1: Count frequencies of each character\n        vector<int> freq(26, 0);\n        for (char c : s) {\n            freq[c - \'a\']++;\n        }\n\n        // Step 2: Create a max-heap based on character\n        priority_queue<pair<char, int>> maxHeap;\n        for (char c = \'z\'; c >= \'a\'; c--) {\n            if (freq[c - \'a\'] > 0) {\n                maxHeap.push({c, freq[c - \'a\']});\n            }\n        }\n\n        string result;\n        \n        // Step 3: Construct the result string\n        while (!maxHeap.empty()) {\n            auto [char1, count1] = maxHeap.top();\n            maxHeap.pop();\n\n            // Determine how many times we can add char1\n            int timesToAdd = min(count1, repeatLimit);\n            result.append(timesToAdd, char1);\n            count1 -= timesToAdd;\n\n            // If we still have more of char1 left, we need to add a different character\n            if (count1 > 0) {\n                if (maxHeap.empty()) {\n                    break; // No more characters to use\n                }\n\n                // Get the next largest character\n                auto [char2, count2] = maxHeap.top();\n                maxHeap.pop();\n\n                // Add one instance of char2\n                result += char2;\n                count2--;\n\n                // If char2 still has remaining instances, push it back to the heap\n                if (count2 > 0) {\n                    maxHeap.push({char2, count2});\n                }\n\n                // Push char1 back to the heap with the remaining count\n                maxHeap.push({char1, count1});\n            }\n        }\n\n        return result;\n    }\n};\n\n```
['Hash Table', 'String', 'Heap (Priority Queue)', 'Counting', 'C++']