v9.1.1
Menu

Longest Repeating Character Replacement

mediumFREESliding WindowBlind 75

Given a string s and an integer k, you can choose any character and change it to any other uppercase English letter at most k times.

Return the length of the longest substring containing the same letter after performing at most k replacements.

Example

Input
AABABBA
1
Output
4
Reasoning
  • We start with the input string AABABBA and the replacement limit k = 1.
  • We try to find the longest substring with the same letter after at most k replacements. The substring AABBB can be formed by replacing one A with a B, resulting in a substring of length 44 with the same letter B if we consider AABBB as BBBB after replacement, but since we are considering AABABBA we look at AABB and BBBA and see AABB can become AAAA or BBBB with one replacement.
  • Considering all possible substrings, we find that the longest one with the same letter after at most 1$ replacement is of length $4$, which can be achieved by replacing one character in AABBto getAAAAor inBBBAto getBBBB`.
  • The final output is 44.

Constraints

  • 1 <= len(s) <= 10^5
  • s consists of uppercase English letters
  • 0 <= k <= len(s)

Learn this in

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.