Skip to content
Decoder

Longest Repeating Character Replacement

Medium

Problem

You are given a string s of uppercase letters and a number k. You may change at most `k` characters to any other uppercase letter. Return the length of the longest substring that can be made of one repeated letter.

Example 1

Input
s = "ABAB", k = 2
Output
4
Why
change both A's or both B's

Example 2

Input
s = "AABABBA", k = 1
Output
4
Why
"AABA" → "AAAA"

Example 3

Input
s = "ABCDE", k = 1
Output
2

Constraints

  • 1 <= s.length <= 10^5
  • s consists of uppercase English letters only.
  • 0 <= k <= s.length

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.