Practice · Sliding window · 2 of 4

Longest Run After k Changes

You are given a string s of uppercase English letters and an integer k. You may change any character of s into any other uppercase letter, at most k times in total.

Return the length of the longest substring that holds a single repeated letter after your changes.

Example 1

Input
s = "ABBCB", k = 1
Output
4
Change the C to B, and BBCB becomes BBBB.

Example 2

Input
s = "AABCAAB", k = 2
Output
6
AABCAA has four As; changing its B and C gives six As in a row.

Example 3

Input
s = "XYZ", k = 0
Output
1

Constraints

  • s holds 1 to 10⁵ letters, each from A to Z.
  • 0 ≤ k ≤ the length of s