#K54207. Longest Substring After K Modifications

    ID: 29702 Type: Default 1000ms 256MiB

Longest Substring After K Modifications

Longest Substring After K Modifications

Given a string s of length n and an integer k, you are allowed to modify at most k characters of the string. The goal is to achieve a longest possible contiguous substring where all characters are the same. In other words, you are to find the maximum length of a substring that can be obtained by changing at most k characters in the original string.

You can think of the problem mathematically as follows:

Find \[ L = \max_{0 \le i \le j < n} \{ j-i+1 \mid \text{the number of characters in } s[i \dots j] \text{ that are not equal to some letter } c \le k \}, \]

A two-pointer (sliding window) technique is very efficient for problems of this nature.

inputFormat

The first line of input contains two integers n and k separated by a space, where n is the length of the string and k is the maximum number of modifications allowed.

The second line contains the string s of length n.

You should read the input from standard input (stdin).

outputFormat

Output a single integer which represents the length of the longest substring that can be achieved where all characters are the same after at most k modifications.

The result should be written to standard output (stdout).

## sample
6 2
abaccc
5