Sliding Window Technique

Sliding Window is a technique but not an algorithm. 

A sliding window is an abstract concept commonly used in array/string problems. A window is a range of elements in the array/string which usually defined by the start and end indices, i.e. [i, j) (left-closed, right-open). A sliding window is a window "slides" its two boundaries to the certain direction. For example, if we slide [i, j) to the right by 1 element, then it becomes [i+1, j+1) (left-closed, right-open).



Sliding window mostly works when you have the variable k (most problems come with k) but also works when you don't have one but could be calculated. 

Click below links for all problems which use Sliding Window Techniques

Longest Substring without Repeating Characters

Maximum Subarray Sum

Longest substring with replacement (#424 Leetcode)

The key to this problem is when the window slides from the left, so tracking the index or count from which the window should slide is crucial. 

The start index would be the start of the window. 

When the window slides from left, we will pop out few elements or one element from the left. So, the window start would change. 




Comments