Skip to content
BASA DSA

Longest Repeating Character Replacement

MediumSliding Window

You may replace at most k characters of s with uppercase letters. Return the greatest length of a contiguous substring that can become one repeated letter.

Example 1

Input
s = "ABBBAC"
k = 1
Output
4

Explanation

Replacing the first A in ABBB gives four B letters; no length-five window needs only one change.

Example 2

Input
s = "ABCDE"
k = 2
Output
3

Explanation

Any three distinct letters can become equal with two changes; four need three.

Example 3

Input
s = "AAAA"
k = 0
Output
4

Explanation

The whole string already contains only A, requiring no replacements.

Constraints

  • 0 <= len(s) <= 100000
  • s contains uppercase English letters
  • 0 <= k <= len(s)

Function contractcharacterReplacementArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…