Mark As Completed Discussion

When to Use A Sliding Window

To identify problems where a sliding window can be helpful, look for a few properties:

  • Whenever you need to calculate a running average
  • If you want to formulate a set of adjacent pairs
  • The problem involves an ordered data structure
  • If you need to identify a target value in an array
  • If you need to identify a longest, shortest, or most optimal sequence that satisfies a given condition in a collection

Let's say we had a string and some characters. We want to find the longest substring (a piece of the string with the same order of characters) containing all of those characters.

SNIPPET
1String: "ADOBECODEBANC"
2Characters:"ABC"
3
4In the above example, it would be "ADOBEC" because it contains A, B, and C.