All problems

Problem 65

Longest Repeating Character Replacement

Medium · 20 pts · String, Sliding Window

Problem

You are given a string S of uppercase letters and an integer K. You may change at most K characters of S, each into any uppercase letter. After the changes, find the longest possible substring that consists of a single repeated letter, and print its length. A window can be turned into one repeated letter when its length minus the count of its most frequent letter is at most K (those are the characters you would change). Slide a window over S, growing it to the right and shrinking it from the left whenever this condition breaks.

Input

First line: string S (uppercase letters). Second line: integer K.

Output

Print the maximum length.

Constraints

1 <= |S| <= 100000; 0 <= K <= |S|

Example input

ABAB
2

Example output

4

Explanation

In "ABAB" with K = 2, changing the two B's to A gives "AAAA" (changing the two A's to B works too). The whole string becomes one letter, so the answer is 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Zigzag Conversion Next: Pangram Check