</>CodexBash
Back to problems
Not started

Best run after k changes

Medium
2000 ms limit256 MB memoryStringsSliding window

Stuck?

Show a hint

A window works when its size minus the count of its most common letter is at most k. Slide the window rather than shrinking it.

Next in this lesson

Two sum in a sorted list

You may change up to k letters of a word to any other letters. Find the length of the longest run of a single repeated letter you can end up with.

Worked example#

For AABABBA with k = 1, a window is usable when the letters inside it that are not the most common one number at most k, because those are the ones you would change:

windowmost commonotherschanges neededusable with k = 1
AABA, twice11yes, length 3
AABAA, three times11yes, length 4
AABABA, three times22no, shrink
ABABA and B, twice each22no, shrink
BABB, twice11yes, length 3
BABBB, three times11yes, length 4
BABBAB, three times22no, shrink

The best is 4.

The trick that keeps this to a single pass: track the count of each letter in the window and the highest count ever seen. The window only needs to grow when a better answer is possible, and a better answer needs a higher count than the best so far, so the highest-count figure never has to be recomputed downward. The window then only ever grows or slides, never shrinks past what it has already proved.

Constraints#

  • 1 <= length <= 100000
  • Uppercase letters only
  • 0 <= k <= length

Input

s is one word of uppercase letters, then k is one integer.

Output

The longest run of one repeated letter that is reachable.

Examples

Input

ABAB
2

Output

4

Change both Bs to As and the whole word is one run.

Input

AABABBA
1

Output

4

Changing the single B in AABA gives AAAA, which is four long.

Input

AAAA
0

Output

4

Already one run, and no changes are needed.

Something wrong with this problem? Report an issue

Ready to try it

Write your solution in C, C++, Java or Python and see every test case come back with the time and memory it took.

Free, and it stays free. No card.