상위 150개 인터뷰쉬움

가장 긴 반복 문자 교체

'가장 긴 반복 문자 교체' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

문자열 s과 정수 k이 제공됩니다. 문자열의 아무 문자나 선택하여 다른 대문자 영문자로 변경할 수 있습니다. 이 작업은 최대 k번 수행할 수 있습니다.

위 작업을 수행한 후 얻을 수 있는 동일한 문자를 포함하는 가장 긴 부분 문자열의 길이를 반환합니다.

characterReplacement(s: str, k: int) -> int 함수를 작성하세요.

제약
  • 1 <= len(s) <= 10^5
  • s consists of only uppercase English letters
  • 0 <= k <= len(s)

Example 1
Input
s = "ABAB", k = 2
Output
4
Explanation

Replace the two 'A's with 'B's or vice versa to get "BBBB" or "AAAA". The longest substring is 4.

Example 2
Input
s = "AABABBA", k = 1
Output
4
Explanation

Replace the 'B' at index 3 with 'A' to get "AAAAABA". The longest substring of same characters starting from index 0 is "AAAA" with length 4.

Need a Hint?
세트나 힙과 같은 슬라이딩 윈도우 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.