Simple solution C++| Java| Python | Javascript
Solution in C++, Python, Java, and JavaScriptIntuitionWe need to construct a string that ensures no character appears more than repeatLimit times consecutively.
\n\n###### *Solution in C++, Python, Java, and JavaScript*\n\n```cpp []\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int repeatLimit) {\n        vector<int> freq(26, 0);\n        for (char c : s) freq[c - \'a\']++;\n        priority_queue<pair<char, int>> pq;\n        for (int i = 0; i < 26; i++) if (freq[i] > 0) pq.push({i + \'a\', freq[i]});\n        string result;\n        while (!pq.empty()) {\n            auto [ch, count] = pq.top(); pq.pop();\n            int use = min(repeatLimit, count);\n            result.append(use, ch);\n            count -= use;\n            if (count > 0) {\n                if (!pq.empty()) {\n                    auto [nextCh, nextCount] = pq.top(); pq.pop();\n                    result.push_back(nextCh);\n                    if (--nextCount > 0) pq.push({nextCh, nextCount});\n                    pq.push({ch, count});\n                }\n            }\n        }\n        return result;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def repeatLimitedString(self, s: str, repeatLimit: int) -> str:\n        from heapq import heapify, heappop, heappush\n        freq = [0] * 26\n        for c in s:\n            freq[ord(c) - ord(\'a\')] += 1\n        pq = [(-i, -count) for i, count in enumerate(freq) if count > 0]\n        heapify(pq)\n        result = []\n        while pq:\n            ch, count = heappop(pq)\n            count = -count\n            use = min(repeatLimit, count)\n            result.append(chr(-ch + ord(\'a\')) * use)\n            count -= use\n            if count > 0:\n                if pq:\n                    nextCh, nextCount = heappop(pq)\n                    result.append(chr(-nextCh + ord(\'a\')))\n                    if nextCount + 1 < 0:\n                        heappush(pq, (nextCh, nextCount + 1))\n                    heappush(pq, (ch, -count))\n        return \'\'.join(result)\n```\n\n```java []\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n        int[] freq = new int[26];\n        for (char c : s.toCharArray()) freq[c - \'a\']++;\n        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[0] - a[0]);\n        for (int i = 0; i < 26; i++) if (freq[i] > 0) pq.add(new int[]{i, freq[i]});\n        StringBuilder result = new StringBuilder();\n        while (!pq.isEmpty()) {\n            int[] curr = pq.poll();\n            int use = Math.min(repeatLimit, curr[1]);\n            result.append(String.valueOf((char) (\'a\' + curr[0])).repeat(use));\n            curr[1] -= use;\n            if (curr[1] > 0 && !pq.isEmpty()) {\n                int[] next = pq.poll();\n                result.append((char) (\'a\' + next[0]));\n                next[1]--;\n                if (next[1] > 0) pq.add(next);\n                pq.add(curr);\n            }\n        }\n        return result.toString();\n    }\n}\n```\n\n```javascript []\nclass Solution {\n    repeatLimitedString(s, repeatLimit) {\n        const freq = Array(26).fill(0);\n        for (const c of s) freq[c.charCodeAt() - 97]++;\n        const pq = [];\n        for (let i = 25; i >= 0; i--) {\n            if (freq[i] > 0) pq.push([String.fromCharCode(97 + i), freq[i]]);\n        }\n        const result = [];\n        while (pq.length) {\n            let [ch, count] = pq.shift();\n            const use = Math.min(repeatLimit, count);\n            result.push(ch.repeat(use));\n            count -= use;\n            if (count > 0 && pq.length) {\n                let [nextCh, nextCount] = pq.shift();\n                result.push(nextCh);\n                if (--nextCount > 0) pq.unshift([nextCh, nextCount]);\n                pq.push([ch, count]);\n            }\n        }\n        return result.join(\'\');\n    }\n}\n```\n\n# Intuition\nWe need to construct a string that ensures no character appears more than `repeatLimit` times consecutively. To make it lexicographically largest, we use characters in descending order.\n\n# Approach\n1. Use a frequency map to count the occurrences of each character.\n2. Use a priority queue (max-heap) to ensure we always pick the lexicographically largest available character.\n3. Append the character up to `repeatLimit` times, and if there are still remaining characters, we insert the next largest available character.\n4. Continue until all valid characters are used.\n\n# Complexity\n- Time complexity: $$O(n \\log k)$$, where `n` is the size of the string and `k` is the distinct character count.\n- Space complexity: $$O(k)$$, where `k` is the number of unique characters.\n\n### *If you have any questions or need further clarification, feel free to drop a comment! \uD83D\uDE0A*
['String', 'Python', 'C++', 'Java', 'JavaScript']