
If the given string S = “abcbacadc” and k = 2.The answer will be 7 corresponding to the length of the substring “abcbaca” as the frequency of characters‘ a’ , ‘b’ and ‘c’, all are greater than or equal to 2.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two integers, ‘N’ denoting the number of characters present in string S and ‘K’.
The following line contains the string ‘S’.
For each test case, print ‘an integer corresponding to the minimum number of swaps required.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^6.
1 <= K <= 1000.
Time limit: 1 sec
In this approach, we will declare an array of size 26 to store the frequency of each letter. We will just try for all possible substrings and check if the substring satisfies the given conditions or not. If it satisfies the condition, we will update the ‘ANS’.
In this approach, we will first decide that we are checking for how many unique characters are in our substring. We will try all numbers from 1 to 26. Then, we will iterate through the string with two pointers
we will declare two pointers ‘i’ and ‘j’ to traverse the string. ‘i‘ and ‘j’ both will be pointing to the first character. We will count ‘UNIQUE’ that will count the number of uniques in the current substring and ‘AT_LEAST_K’ to store the number of characters having a frequency greater than K.
While j is less than N:
If UNIQUE is less than the fixed Unique:
We will add a character to the window and set j as j +1.
Else:
We will remove a character from the window and set i as i +1.
After this, we will update the ‘FREQ’ array and ‘UNIQUE’ and ‘AT_LEAST_K’ count.
If UNIQUE count is equal to fixed unique count and UNIQUE count is equal to ‘AT_LEAST_K’.We found a suitable substring. Update ‘ANS’.
At last, we will return ‘ANS’ storing the length of the longest substring.