Online Judge/LeetCode
[LeetCode][Python] 424. Longest Repeating Character Replacement
vince joe
2024. 9. 9. 23:42
- 문제 링크: https://leetcode.com/problems/longest-repeating-character-replacement/
- 난이도: Medium
주어진 문자열에서 k개까지 문자를 자유롭게 바꿀 수 있을 때, 가장 긴 반복되는 문자열의 길이를 구하는 문제다.
요거 설명은.. 유튜브 영상으로 대체
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
histogram = defaultdict(int)
l = 0
max_count = 0
for r in range(len(s)):
histogram[s[r]] += 1
max_count = max(max_count, histogram[s[r]])
if (r - l + 1) - max_count > k:
histogram[s[l]] -= 1
l += 1
return r - l + 1
반응형