I don’t understand this line.

maxLength = Math.max(maxLength, windowEnd - windowStart + 1);

The above code runs without any condition and even if it brings the correct answer always. This article lacks proper explanation.

I don’t understand this line.

maxLength = Math.max(maxLength, windowEnd - windowStart + 1);

The above code runs without any condition and even if it brings the correct answer always. This article lacks proper explanation.

“windowEnd - windowStart + 1” represents the length of the current window. So this line is finding the maximum of ‘maxLength’ and length of the current window, and storing it in ‘maxLength’.

That is where exactly I have a doubt. How we are deciding to shrink the window dynamically in the below code?

if (windowEnd - windowStart + 1 - maxRepeatLetterCount > k) {

//shrink window

}

so we have window size as (windowEnd - windowStart + 1), then we subtract maxRepeatLetterCount.

Can you show an example or dry run, how this will work? I fail to understand how we have decided to shrink the window based on this condition.

Example input String=“aabccbb”, k=2

No reply?

You should try a dry run to see how things are working.

Following code comment tells us why/when we need to shrink the window:

// if the remaining letters are more than ‘k’, it is the time to shrink the window as we

// are not allowed to replace more than ‘k’ letters

Following code is evaluate the above mentioned condition:

if (windowEnd - windowStart + 1 - maxRepeatLetterCount > k) {

//shrink window

}