贪心,每次都尽量取大的,除非连续取的次数超出限制,此时取一个下一个字符
class Solution:def repeatLimitedString(self, s: str, repeatLimit: int) -> str:N = 26count = [0] * Nfor c in s:count[ord(c) - ord('a')] += 1ret = []i, j, m = N - 1, N - 2, 0while i >= 0 and j >= 0:if count[i] == 0: # 当前字符已经填完,填入后面的字符,重置 mm, i = 0, i - 1elif m < repeatLimit: # 当前字符未超过限制count[i] -= 1ret.append(chr(ord('a') + i))m += 1elif j >= i or count[j] == 0: # 当前字符已经超过限制,查找可填入的其他字符j -= 1else: # 当前字符已经超过限制,填入其他字符,并且重置 mcount[j] -= 1ret.append(chr(ord('a') + j))m = 0return ''.join(ret)