传统滑动窗口可以解决问题,看代码
O(n)
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
l, r = 0, 0
res = 0
if not s:
return 0
cnt = collections.Counter(s[l])
while r < len(s) - 1:
r += 1
cnt[s[r]] += 1
max_cnt_num = cnt.most_common(1)[0][1]
while r - l + 1 - max_cnt_num > k:
cnt[s[l]] -= 1
l += 1
res = max(res, r - l + 1)
return res