#K55002. Maximum Contiguous Substring Length After Operations
Maximum Contiguous Substring Length After Operations
Maximum Contiguous Substring Length After Operations
You are given a string S consisting only of characters A and B. In one operation, you can change any character in the string to the other character. You are also given an integer K, representing the maximum number of operations allowed.
Your task is to determine the maximum length of a contiguous substring consisting of the same character that can be obtained after performing at most K operations.
The solution involves using the sliding window technique. For each test case, you should try to maximize the substring length by considering both characters A and B.
The problem can be mathematically formalized as follows:
For a given string \(S\) of length \(N\) and an integer \(K\), find the maximum \(L\) such that there exists a contiguous segment of \(S\) with length \(L\) that can be transformed into a string with all characters equal by at most \(K\) operations.
Note: Use the following sliding window approach: Increase the window while the number of changes required does not exceed \(K\) (i.e., when the number of mismatches with the target character is \(\leq K\)). Otherwise, move the left boundary. The answer for each test case is the maximum window size achieved.
inputFormat
The first line of input contains an integer \(T\) denoting the number of test cases.
Each test case is given on a separate line and consists of two integers \(N\) and \(K\) followed by a string \(S\) of length \(N\) (containing only characters 'A' and 'B').
Example:
3 5 1 AABAB 4 2 BBAB 6 0 ABABAB
outputFormat
For each test case, output a single integer representing the maximum length of a contiguous substring consisting of the same character after performing at most \(K\) operations. Each result should be printed on a new line.
For the above example, the output should be:
4 4 1## sample
3
5 1 AABAB
4 2 BBAB
6 0 ABABAB
4
4
1
</p>