Easy Solution✅| Tree Map✅| O(N*K)✅| Java Solution✅| 
Intuition
The goal is to construct the lexicographically largest string under the condition that no character can repeat more than repeatLimit times consecutive
# Intuition\n- The goal is to construct the lexicographically largest string under the condition that no character can repeat more than repeatLimit times consecutively. To achieve this:\n\n1. **Prioritize the largest characters first to make the string lexicographically larger.**\n1. **Ensure that a character is appended at most repeatLimit times consecutively. If the character appears more frequently, we must "interrupt" it with the next largest character.**\n1. **Repeat this process until all characters are used or no valid interruption is possible.**\n\n# Approach\n1. **Count Frequencies in Reverse Lexicographic Order:**\n\n- Use a TreeMap with Collections.reverseOrder() to store characters in descending order. This ensures that the largest characters are processed first.\nPopulate the TreeMap with the frequency of each character.\n2. **Prepare a List of Characters:**\n\n- Extract the keys (characters) of the TreeMap into a List. This allows us to iterate through the largest characters easily.\n3. **Construct the Result String:**\n\n- Use a StringBuilder to construct the result string efficiently.\nProcess the largest character (ch) \n- - first:\nIf its frequency (val) is greater than or equal to repeatLimit, append it repeatLimit times.\nReduce its count by repeatLimit. If the count becomes zero, remove it from the map and list.\n- - - If the count is not zero, "interrupt" the sequence by appending the next largest character (ch2) from the list.\nDecrement the frequency of ch2. If its count becomes zero, remove it from the map and list.\n- - - If no second character (ch2) is available, break the loop as no valid interruption can occur.\n4. **Edge Case for Frequency < RepeatLimit:**\n\n- If the frequency of a character is less than repeatLimit, append the character as many times as its frequency and remove it from the map and list.\n5. **Return Result:**\n\n- Finally, return the string constructed using the StringBuilder.\n\n# Complexity\n- Time complexity: **O(N*K)**\n\n- Space complexity: **O(N+K)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n        Map<Character,Integer> map=new TreeMap<>(Collections.reverseOrder());\n        for(int i=0;i<s.length();i++){\n            map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);\n        }\n        List<Character> list=new ArrayList<>();\n        for(Character i : map.keySet()){\n            list.add(i);\n        }\n        StringBuilder stb=new StringBuilder();\n        while(!list.isEmpty()){\n            char ch = list.get(0);\n            int val=map.get(ch);\n            if(val>=repeatLimit){\n                for(int i=0;i<repeatLimit;i++){\n                    stb.append(ch);\n                }\n                map.put(ch,val-repeatLimit);\n                if(map.get(ch)==0){\n                    map.remove(ch);\n                    list.remove(0);\n                }\n                else{\n                    if(list.size()>1){\n                        char ch2=list.get(1);\n                        stb.append(ch2);\n                        map.put(ch2,map.get(ch2)-1);\n                        if(map.get(ch2)==0){\n                            map.remove(ch2);\n                            list.remove(1);\n                        }\n                    }\n                    else{\n                        break;\n                    }\n                }\n            }\n            else{\n                for(int i=0;i<val;i++){\n                    stb.append(ch);\n                }\n                map.remove(ch);\n                list.remove(0);\n            }\n        }\n        return stb.toString();\n    }\n}\n```
['Java']