- ํฌ ํฌ์ธํฐ์ ์ฌ๋ผ์ด์ฑ ์๋์ฐ, Counter ๊ธฐ๋ฒ ๋ชจ๋ ์ฌ์ฉํด์ผ ์๊ฐ ์ด๊ณผ๊ฐ ๋์ง ์์
Solution
import collections
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
answer = 0
lt, rt = 0, 0
counter = collections.Counter()
while rt < len(s):
counter[s[rt]] += 1
# lt๋ถํฐ rt๊น์ง ๊ฐ์ฅ ๋ง์ ๋ฌธ์์ ๊ฐ์
most_common = counter.most_common(1)[0][1]
# ๋ฐ๊ฟ์ผ ํ ๋ฌธ์ ์
remain = rt - lt + 1 - most_common
# ๋ฐ๊ฟ์ผ ํ ๋ฌธ์ ์๊ฐ ๋ฐ๊ฟ ์ ์๋ ๋ฌธ์ ์๋ณด๋ค ๋ง์ ๋
if remain > k:
counter[s[lt]] -= 1
# lt๋ฅผ ์ฆ๊ฐ์์ผ ์๋์ฐ ์๋ฅผ ์ค์
lt += 1
answer = max(rt - lt + 1, answer)
rt += 1
return answer
Reference
Leave a comment