can you please explain the logic behind the editorial solution.
Logic behind the editoriol solution
Basically, there is a string which consists of only a and b. A substring is a contiguous sequence of characters within a string. Our aim is to generate substrings of a and b by swapping characters such that the lengths of substrings of either a or b is maximum possible. The only constraint we have here is that only k swaps are allowed. So, you have to tell the maximum possible length of the substrings that can be generated. By swapping, we mean that a can be replaced by b and b can be replaced by a.
for(int i=0;i<n;i++){
arr[s[i]-'a']++;
while(min(arr[0],arr[1])>k){
arr[s[j++]-'a']--;
len--;
}
maxLen=max(maxLen,++len);
}
u are basically mantaining track of no of as or bs that have come in the subsequence
if either of the freq becomes greater than k then left pointer needs to be incremeneted so that the no of swap reduce until it is less than greatest swap permisable
try a dry run on pen paper for
abbbbaab k = 2
abaabbaaab k = 3
u`ll understand the code logic better