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
Change the C to an A and indices 1 to 5 read AAAAA. Six letters would need two changes: indices 0 to 5 hold a B and the C, and indices 1 to 6 hold the C and the last B.

lock icon+17 hidden tests on Submit

challenge icon

Follow-up

What changes if s can hold any character, not only the 26 uppercase letters?

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

Case 1

Case 2

Case 3

Input

s = "BAAACAB"
k = 1

Expected

5