2182. Construct String With Repeat Limit
## 思路
先计算字符出现次数
用max_heap 存 (-ord(ch), freq)
每次pop找下一个要印出的字符, 最多印出repeatLimit 剩下的再塞回heap
如果pop的字符跟前面的相同就先印出另1个字符
e.g {'z': 4, 'x' 3} repeatLimit=3
zzz x z xx
## Code
```python
class Solution:
def repeatLimitedString(self, s: str, repeatLimit: int) -> str:
counter = Counter(s)
max_heap = [(-ord(ch), freq) for ch, freq in counter.items()]
heapq.heapify(max_heap)
ans = []
prev = None
while max_heap:
ch_ansi, freq = heapq.heappop(max_heap)
if prev == ch_ansi:
if not max_heap:
break
next_ch_ansi, next_freq = heapq.heappop(max_heap)
heapq.heappush(max_heap, (ch_ansi, freq))
ans.append(chr(-next_ch_ansi))
if next_freq > 1:
heapq.heappush(max_heap, (next_ch_ansi, next_freq-1))
prev = next_ch_ansi
else:
ans.append(chr(-ch_ansi) * min(freq, repeatLimit))
if freq > repeatLimit:
heapq.heappush(max_heap, (ch_ansi, freq-repeatLimit))
prev = ch_ansi
return ''.join(ans)
```