Sliding window algorithm
Principle
The idea of the sliding window algorithm is this:
We use the left and right pointer trick in the double pointer in string S, initialize left = right = 0, and call the index closed interval [left, right] a “window”.
We first keep increasing the right pointer to expand the window [left, right] until the string in the window meets the requirements (including all the characters in T).
At this point, we stop adding right, and instead keep increasing the left pointer to shrink the window [left, right] until the string in the window no longer meets the requirements (does not contain all the characters in T). At the same time, every time we increase left, we have to update the result one round.
Repeat steps 2 and 3 until the end of string S is reached to the right.