[Python3] Heap approach, beats 100% in time
\n# Code\n\nclass Solution:\n    def repeatLimitedString(self, s: str, repeatLimit: int) -> str:\n        # Create max heap based on the lexigraphic order and n
![image.png](https://assets.leetcode.com/users/images/a5b19f92-b415-4e1b-b4fe-2220ee0fc470_1704472531.165642.png)\n# Code\n```\nclass Solution:\n    def repeatLimitedString(self, s: str, repeatLimit: int) -> str:\n        # Create max heap based on the lexigraphic order and number of occurrences   \n        heap = [\n            (-ord(char), -count) \n            for char, count in Counter(s).items()\n        ]\n        heapq.heapify(heap)\n\n        res = \'\'\n        while heap:\n            # Take out the largest letter\n            max_char, max_count = heapq.heappop(heap)\n            while heap and -max_count > repeatLimit:\n                # When the largest letter is appended *repeatLimit* times\n                # insert ONE second largest letter\n                top_char, top_count = heapq.heappop(heap)\n                res += chr(-max_char) * repeatLimit + chr(-top_char)\n                max_count += repeatLimit\n\n                # If the second largest letter is all used up\n                # don\'t push it back to the heap\n                if top_count < -1:\n                    heapq.heappush(heap, (top_char, top_count+1))\n            # Appends the remaining counting to the result\n            res += chr(-max_char) * min(-max_count, repeatLimit)\n        return res\n```
['Heap (Priority Queue)', 'Python3']