Skip to main content

Longest Repeating Character Replacement

You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English letter. You can perform this operation at most k times. Return the length of the longest substring containing the same letter you can get after performing the above operations.

Problems49 / 152
Sign in

Longest Repeating Character Replacement

MEDIUMStringSliding Window

You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English letter. You can perform this operation at most k times. Return the length of the longest substring containing the same letter you can get after performing the above operations.

Example 1

Input:  s = "ABAB", k = 2
Output: 4

Example 2

Input:  s = "AABABBA", k = 1
Output: 4
Show hint

Use sliding window. Track frequency of each character. Window is valid if (window_length - max_frequency) <= k. Expand right, shrink left when invalid.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
s =
"ABAB"
k =
2
Expected
4
Run with your own input (JSON)