Java solution-->(Classic implementation of Map+PriorityQueue)
In the priority queue sorting part, Character.compare(b,a) can be replaced by return b-a; which reduces the runtime \n\n\nclass pair{\n    Character ele;\n    i
# In the priority queue sorting part, Character.compare(b,a) can be replaced by return b-a; which reduces the runtime \n\n```\nclass pair{\n    Character ele;\n    int freq;\n    pair(Character ele, int freq){\n        this.ele = ele;\n        this.freq = freq;\n    }\n}\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n        PriorityQueue<pair> pq = new PriorityQueue<>((a,b) -> {\n            return Character.compare(b.ele, a.ele);\n        });\n        Map<Character, Integer> map = new HashMap<>();\n        for(char c : s.toCharArray()){\n            map.put(c, map.getOrDefault(c,0)+1);\n        }\n        StringBuilder sb = new StringBuilder();\n        for(Map.Entry<Character, Integer> entry : map.entrySet()){\n            pair p = new pair(entry.getKey(), entry.getValue());\n            pq.offer(p);\n        }\n        \n        while(!pq.isEmpty()){\n            pair LexicographicallyLargest = pq.poll();\n            int frequency = LexicographicallyLargest.freq;\n            for(int i = 0;i<repeatLimit;i++){\n                sb.append(LexicographicallyLargest.ele);\n                LexicographicallyLargest.freq--;\n                if(LexicographicallyLargest.freq == 0){\n                    break;\n                }\n            }\n            if(LexicographicallyLargest.freq > 0){\n                if(!pq.isEmpty()){\n                    pair LexicographicallySecondLargest = pq.poll();\n                    sb.append(LexicographicallySecondLargest.ele);\n                    LexicographicallySecondLargest.freq--;\n                    if(LexicographicallySecondLargest.freq != 0){\n                        pq.offer(LexicographicallySecondLargest);\n                    }\n                    pq.offer(LexicographicallyLargest);\n                }\n            }\n        }\n        return sb.toString();\n    }\n}\n```
['Heap (Priority Queue)', 'Java']