Simple Solution using PriorityQueue.
IntuitionThe problem involves creating a lexicographically largest string with a given constraint: no character should repeat more than repeatLimit times consec
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves creating a lexicographically largest string with a given constraint: no character should repeat more than `repeatLimit` times consecutively. This requires:\n\n- Tracking the frequency of each character.\n- Using a priority mechanism to always pick the largest valid character available while adhering to the repeat limit.\n\nThe approach leverages a **max-heap (PriorityQueue)** to maintain the order of characters by their frequency and lexicographical value. By repeatedly processing the most frequent characters and switching to the next available character when the limit is reached, the constraints are satisfied efficiently.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Frequency Count:**\nCreate a frequency array for all characters in the input string s.\n\n2. **Max-Heap Initialization:**\nPopulate a max-heap (priority queue) where each element is a Pair containing the character and its frequency. The heap orders characters by their lexicographical order and frequency (highest first).\n\n3. **String Construction:**\n- Extract the most frequent character from the heap and append it to the result string up to repeatLimit times.\n- If the character\'s frequency exceeds repeatLimit, place it back in the heap after reducing its frequency.\n- If the limit is reached, append the next largest character available in the heap to avoid consecutive repetition.\n- Repeat until the heap is empty.\n\n4. **Handle Remaining Characters:**\nAny character with leftover frequency is re-added to the heap to be processed in subsequent iterations.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- - Constructing the frequency array: \uD835\uDC42(\uD835\uDC5B), where \uD835\uDC5B is the length of the input string `s`.\n- - Initializing the max-heap: \uD835\uDC42(\uD835\uDC58log\uD835\uDC58), where \uD835\uDC58 is the number of unique characters (at most 26 for lowercase English letters).\n- - Processing the heap: \uD835\uDC42(\uD835\uDC5Blog\uD835\uDC58), since each character insertion/removal from the heap is \uD835\uDC42(log\uD835\uDC58), and we process each character in the string.\n- - Overall: \uD835\uDC42(\uD835\uDC5Blog\uD835\uDC58), which simplifies to \uD835\uDC42(\uD835\uDC5B) for this problem as \uD835\uDC58 is a small constant (26).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- - Frequency array: \uD835\uDC42(26)=\uD835\uDC42(1).\n- - Max-heap storage: \uD835\uDC42(\uD835\uDC58), where \uD835\uDC58\u226426.\n- - Result string: \uD835\uDC42(\uD835\uDC5B).\n- - Overall: \uD835\uDC42(\uD835\uDC5B).\n\n# Code\n```java []\nclass Solution {\n    static class Pair implements Comparable<Pair>{\n        char ch;\n        int freq;\n\n        public Pair(char c, int f){\n            ch = c;\n            freq = f;\n        }\n\n        @Override\n        public int compareTo(Pair b){\n            return (int)this.ch - b.ch;\n        }\n    }\n    public String repeatLimitedString(String s, int repeatLimit) {\n        int[] freq = new int[26];\n        for(char c : s.toCharArray()){\n            freq[c - \'a\']++;\n        }\n        StringBuilder sb = new StringBuilder();\n        PriorityQueue<Pair> pq = new PriorityQueue<>(Collections.reverseOrder());\n        for(int i=0; i<26; i++){\n            if(freq[i] != 0)\n                pq.add(new Pair((char)(i+\'a\'), freq[i]));\n        }\n        while(!pq.isEmpty()){\n            Pair p = pq.poll();\n            int count = Math.min(p.freq, repeatLimit);\n            sb.append(String.valueOf(p.ch).repeat(count));\n            p.freq -= count;\n            if(p.freq > 0){\n                if(!pq.isEmpty()){\n                    Pair p2 = pq.poll();\n                    sb.append(p2.ch);\n                    p2.freq--;\n                    if(p2.freq > 0)\n                        pq.add(p2);\n                    pq.add(p);\n                }\n            }\n        }\n        return sb.toString();\n\n    }\n}\n```
['Java']