Python 3 | Priority queue | Easy understanding with comment
Approach\n Describe your approach to solving the problem. \nFirst we store all the characters in a heap.\nThen we pop them one by one to add them into the answe
# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst we store all the characters in a heap.\nThen we pop them one by one to add them into the answer.\nIf the count of the current character is smaller than limit, add it to the answer directly.\nIf the count of the current character is larger than limit, we add it to the answer with count limit. Then we can add one single next character to maximize the string. So:\n-- if there\'s nothing in the heap anymore, then we have the answer since we can\'t add anymore characters to the answer.\n-- if there\'s still characters in the heap, then we can use one of the maximum character to seperate current character.\n\n# Code\n```\nclass Solution:\n    def repeatLimitedString(self, s: str, repeatLimit: int) -> str: \n        # Python only have min heap\n        heap = [(-ord(key), value) for key, value in Counter(s).items()]\n        heapq.heapify(heap)\n        ans = ""\n\n        while heap:\n            # The current character\n            k, v = heapq.heappop(heap)\n            if v <= repeatLimit:\n                # Add all this character\n                ans += chr(-k) * v\n            else:\n                # Only add limit numbers of character\n                ans += chr(-k) * repeatLimit\n                # If nothing in the heap (no next character), then we have the answer\n                if not heap:\n                    break\n                # The next character\n                nk, nv = heapq.heappop(heap)\n                # Add one to the answer\n                ans += chr(-nk)\n                # If there\'s still next character remaining, add it back to the heap\n                if nv > 1:\n                    heapq.heappush(heap, (nk, nv - 1))\n                # Add the remaining current character back to the heap\n                heapq.heappush(heap, (k, v - repeatLimit))\n        return ans\n```
['String', 'Heap (Priority Queue)', 'Counting', 'Python3']