Sanket And Strings

for(int r=0;r<str.length();r++){
char a = str[r];
count[a - ‘a’]++;
if(min(count[0], count[1]) > k){
count[str[l] - ‘a’]–;
l++;
}
else
ans++;
}
Unable to understand the for loop’s working

For each character in the input String, the idea is to keep an array counting the number of 'a' and 'b' between a given left pointer, that will represent the start of the substring, and the current character.

When the count of each 'a' or of each 'b' is less than the allowed number of characters to change, we can consider making the change and incrementing the answer. Then, when both of those 2 counts becomes greater than the allowed number of characters to change, it means we cannot change more characters so we hit a maximum length: the left pointer is increased, and the count of the character at that pointer is decreased.

The count of 'a' and 'b' characters can be modeled as an integer array (index 0 corresponds to the count of 'a' while index 1 corresponds to the count of 'b' ).