Identifying variable sized sliding window pattern


Some specific problems where we need to remember the occurrences of data items in all windows in a sequence can be solved using the variable sized sliding window technique. These are generally medium or hard problems where we need to make some critical observations to prove that skipping some windows does not affect the correctness of the solution. We then create a sliding window and identify when to expand or contract it (or move it forward by doing both in the same iteration) through the sequence while updating the associated hash map to always map all the data items in the current window to some values (for example, the frequency of each item).

If the problem statement or its solution follows the generic template below, it can be solved by applying the variable-sized sliding window technique.

Template:
Given an iterable sequence of data, for all windows of all sizes, map all the data items in a window to some values and use them to solve the problem.

Example

Let's consider the following problem as an example to better understand how to identify and solve a problem using the variable-sized sliding window technique.

Problem statement: Given a string `s`, find the length of the longest substring without any repeating characters

Find the length of the longest substring without any repeating characters.

Liking the course? Check our discounted plans to continue learning.