Editor

Longest Run After K Changes

#148 · Sliding window · Advanced · 135 XP · about 25 min

s is a string of uppercase letters. You may change up to k of its characters, each into any uppercase letter you like. Write longest_after_changes(s, k) that returns the length of the longest block of one repeated letter you can make.

Examples

longest_after_changes('ABBCB', 1)   # returns 4
longest_after_changes('XYZ', 0)     # returns 1
longest_after_changes('AAAB', 5)    # returns 4

In 'ABBCB', changing the C to B turns the last four characters into 'BBBB'. A block can never be longer than the string itself, however many changes you have.

Constraints

  • 0 <= len(s) <= 100,000
  • 0 <= k <= 100,000
  • s holds only the letters A to Z

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.