Skills101
← All coding problems

Longest Repeating Character Replacement

mediumSliding Window+20 XPTime limit 2s

You may change at most k characters of the string into any other uppercase letter. What is the length of the longest substring that can be made of one repeated letter?

Input:

  • line 1: s (uppercase English letters)
  • line 2: k

Output: the maximum length.

Constraints: 1 ≤ length of s ≤ 10⁵, 0 ≤ k ≤ length of s.

Think about it: a window is fixable when window length − (count of its most frequent letter) ≤ k. Slide the window and keep letter counts.

Sample input 1

ABAB
2

Sample output 1

4

Sample input 2

AABABBA
1

Sample output 2

4

📖 Stuck? Learn the pattern

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.