Menu
CoddyTech

Longest Repeating Character Replacement

You get a string s of uppercase English letters and an integer k. You may pick at most k positions of s and change the letter at each one to any other uppercase letter.

Return the length of the longest substring, a run of letters that sit next to each other, that holds a single repeated letter after your changes.

Function

characterReplacement(s: string, k: integer) → integer
sstring
the string of uppercase letters
kinteger
the most letters you may change
Returnsinteger
the length of the longest substring of one repeated letter you can make

Constraints

  • 1 ≤ s.length ≤ 5 × 104
  • s holds only uppercase English letters.
  • 0 ≤ k ≤ s.length

Examples

Input
s = "BAAACAB"k = 1
Output
5
Explanation
Changing the C to A makes indices 1 to 5 read AAAAA. Both six-letter windows hold a B and the C, so they need two changes.
challenge icon

Follow-up

What changes if s can hold any character instead of the 26 uppercase letters?

Reset code
def characterReplacement(s, k):
    # Write code here

Test #1

Test #2

Test #3

Arguments

s = "BAAACAB"
k = 1

Expected Output

5