problems / 142

Uniform After K Repaints

medium sliding-window two-pointers strings 1000 ms · 256 MB

Uniform After K Repaints

You are given a string s of n uppercase English letters and an integer k.

You may repaint at most k positions of s: each repaint replaces the letter at one position with any uppercase letter of your choice. After repainting, look for the longest contiguous block of positions that all hold the same letter.

What is the largest length that block can have?

Input

The first line contains two integers n and k (1 ≤ n ≤ 10^6, 0 ≤ k ≤ n).

The second line contains the string s of exactly n uppercase English letters (A to Z).

Output

Print one integer: the maximum possible length of a block of equal consecutive letters after at most k repaints.

Notes

In the first example, AABABBA with k = 1: repainting the A at position 4 (1-based) gives AABBBBA, which has a block of four Bs. No single repaint produces a block of five.

In the second example, with k = 2 you can turn ABAB into AAAA (or BBBB), so the answer is the whole string.

Example 1

Input

7 1
AABABBA

Output

4

Example 2

Input

4 2
ABAB

Output

4

Hints · reveal one at a time if you're stuck

Hint 1

Fix a block of positions you want to make uniform. How many repaints does it cost, given the letters it already contains? Think about which letter to keep.

Hint 2

A block is achievable exactly when its length minus the count of its most frequent letter is at most k. This condition only gets easier for smaller blocks, which suggests a sliding window over the string.

Hint 3

Use two pointers in a single pass, tracking the 26 letter counts in the window and the largest count seen. Let the window grow when it is valid and slide when it is not, so it never shrinks; the answer is then its final size.

report an issue Prefer your own editor? sooks prep 142 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.