📘
Longest Repeating Character Replacement
MediumSliding Window
Given a string s and an integer k, you can choose any character and change it to any other uppercase English letter at most k times.
Return the length of the longest substring containing the same letter after performing at most k replacements.
Example:
Input:
AABABBA 1
Output:
4
Reasoning:
- We start with the input string
AABABBAand the replacement limitk = 1. - We try to find the longest substring with the same letter after at most
kreplacements. The substringAABBBcan be formed by replacing oneAwith aB, resulting in a substring of length 4 with the same letterBif we considerAABBBasBBBBafter replacement, but since we are consideringAABABBAwe look atAABBandBBBAand seeAABBcan becomeAAAAorBBBBwith one replacement. - Considering all possible substrings, we find that the longest one with the same letter after at most
1$ replacement is of length $4$, which can be achieved by replacing one character inAABBto getAAAAor inBBBAto getBBBB`. - The final output is 4.
Constraints:
- 1 <= len(s) <= 10^5
- s consists of uppercase English letters
- 0 <= k <= len(s)
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.