问题
解决
class Solution:
def repeatLimitedString(self, s: str, repeatLimit: int) -> str:
freq=Counter(s)
ans=list()
cnt=1
right=2
for c in range(25,-1,-1):
ch=chr(ord("a")+c)
ans.extend([ch]*freq[ch])
for left in range(1,len(ans)):
if ans[left-1]!=ans[left]:
cnt=1
else:
cnt+=1
if cnt>repeatLimit:
right = max(right, left + 1)
while right<len(ans) and ans[left]==ans[right]:
right+=1
if right < len(ans):
ans[left], ans[right] = ans[right], ans[left]
cnt = 1
else:
ans = ans[:left]
break
return "".join(ans)
|