Problem · String
Longest Repeating Character Replacement
Learn this problemProblem statement
You are given a string s containing only uppercase English letters and an integer k. You may replace at most k characters in s with any other uppercase English letter.
Return the length of the longest substring that can contain only one repeated letter after at most k replacements.
Function
characterReplacement(s: String, k: int) → intExamples
Example 1
s = "ABAB"k = 2return = 4Replace both occurrences of A with B, or both occurrences of B with A. The entire string then contains one repeated letter.
Example 2
s = "AABABBA"k = 1return = 4The substring AABA can become AAAA with one replacement, so a length of 4 is possible.
Constraints
1 <= s.length <= 100000scontains only uppercase English letters.0 <= k <= s.length