// If the char at previous position of left pointer was not ch, then that
// position must
// have counted as a swap earlier. Now we have a free swap available.
// Iterate right pointer forward to use that one free swap
if (j < s.length() - 1 && s.charAt(i - 1) != ch) {
j++;
}
This part of code is unclear to me
Hi @ssakshamprime,
This part suggest that if the character at the left pointer is not equal to ch then we would have already counted this character and increased c or count. hence decreasing a count because of left pointer and then increasing a count for the right pointer will not change the total number of swaps or count value hence this is a free swap as one is decreasing the count value and the other is increasing the count value…therefore count value remains the same. I hope this made it clear. If not then try to dry run the code once with paper and pen and you will understand the flow.
Can you suggest any test case for this?
Yeah got it, at this point j will be pointing to a character != ‘ch’ so we are just incrementing j to increase the size of window.
Nice … If you still need test case then
2
aaabbbbbbaabbb
Take this as test case the answer is 11.