Problem · String
Longest Repeating Character Replacement
Learn this problemProblem statement
Given a string s and an integer k, you may replace at most k characters with other uppercase English letters. Return the maximum length of a contiguous substring that can contain only one repeated character after these replacements.
For this exercise, assume the input contains only uppercase English letters. Return 0 for an empty string; replacements may be unused.
Function
characterReplacement(s: String, k: int) → intExamples
Example 1
s = "ABAB"k = 2return = 4Replace both B characters to obtain AAAA.
Example 2
s = "AABABBA"k = 1return = 4The first four characters AABA can become AAAA with one replacement. Every length-five window needs at least two.
Example 3
s = ""k = 0return = 0An empty string has length zero.
Constraints
0 <= s.length <= 10^5.0 <= k <= s.length.- Every character is between
AandZ.