Longest Repeat With k Changes

MediumSliding Window≈ 35 min

You are given a string text of capital letters A–Z and a number k. You may change at most k letters of the text, each into any other capital letter. Return the length of the longest run of one repeated letter you can make.

Examples

text
text = "ABAB", k = 2      →  4
text = "AABABBA", k = 1   →  4
text = "ABCDE", k = 0     →  1
text = "AAAA", k = 2      →  4

In the first example, changing both Bs gives "AAAA". In the second, changing the A at index 3 gives "AABBBBA", with four Bs in a row. With k = 0 nothing can change, so the answer is the longest run already there.

Constraints

  • Between 0 and 20,000 letters; k is between 0 and 20,000.
  • Aim for O(n): there are 26 letters, so counting them is a fixed amount of work per step.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.