MediumHash TableStringSliding Window

1 approach, code in every language

Problem description

Given a string s of uppercase English letters and an integer k, you may choose at most k positions and overwrite each with any uppercase letter of your choice. Return the length of the longest substring that can be made into a single repeated letter after those changes.

You do not have to use all k changes.

Example 1

Input: s = "ABAB", k = 2

Output: 4

Change the two B's into A's (or the two A's into B's) to make the whole string uniform.

Example 2

Input: s = "AABABBA", k = 1

Output: 4

One change turns a window like "AABA" into "AAAA", giving a run of length four.

Constraints

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

After the pattern

Now solve one you haven't seen.

Longest Repainted Stretch is our own problem on the technique behind Longest Repeating Character Replacement: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free