Skip to main content

Command Palette

Search for a command to run...

Sliding Window DSA All Patterns & How to recognize

Published
3 min read
Sliding Window DSA All Patterns & How to recognize
S

Ex Full Stack Developer at @WiseBoxs | Vue React Node | MERN

🧩 Pattern 1: Fixed-Size Sliding Window

When to recognize it

  • Window size is given or constant

  • You don’t need to grow or shrink dynamically

Keywords

  • “subarray of size k”

  • “exactly k length”

  • “window size is k”

Template

expand right
remove left (when window size > k)
update answer

Examples

  • Maximum sum subarray of size k

  • First negative number in every window of size k

  • Sliding window average

TC

O(n)

🧩 Pattern 2: Variable Window – Constraint Based

When to recognize it

  • Window size is not fixed

  • You shrink only when a condition is violated

Keywords

  • “at most k”

  • “no more than”

  • “sum ≤ k”

  • “distinct ≤ k”

Key Property

Window validity is monotonic

Template

expand right
while (condition violated)
    shrink left
update answer

Examples

  • Longest substring with at most K distinct characters

  • Subarrays with sum ≤ K

  • Fruit Into Baskets


🧩 Pattern 3: Exactly K = AtMost(K) − AtMost(K−1)

When to recognize it

  • Problem says “exactly k”

  • Counting subarrays (not longest)

Keywords

  • “exactly k distinct”

  • “exactly k odd numbers”

Trick

Exactly K = AtMost(K) - AtMost(K-1)

Examples

  • Subarrays with K distinct integers

  • Nice subarrays (k odd numbers)

TC

O(n)

🧩 Pattern 4: Substring with Required Characters (Frequency Matching)

When to recognize it

  • Order does NOT matter

  • Frequency matters

  • Characters must be present

Keywords

  • “contains all characters”

  • “minimum window substring”

  • “permutation”

Key Idea

  • Use frequency map

  • Track formed === required

Examples

  • Minimum Window Substring

  • Find All Anagrams in a String

  • Permutation in String


🧩 Pattern 5: Longest Window with Constraint

When to recognize it

  • Asked for maximum length

  • Window stays valid as long as constraint holds

Keywords

  • “longest”

  • “maximum length”

  • “replace at most k characters”

Examples

  • Longest Repeating Character Replacement

  • Longest Substring Without Repeating Characters

Trick

  • Don’t shrink aggressively

  • Track best length


🧩 Pattern 6: Forward + Backward Window (Subsequence)

When to recognize it

  • Order matters

  • Window validity is not monotonic

  • Subsequence, not substring

Keywords

  • “subsequence”

  • “characters must appear in order”

  • “minimum window subsequence”

Strategy

  1. Forward scan → find valid window

  2. Backward scan → minimize window

Example

  • Minimum Window Subsequence

TC

O(n × m)

🧩 Pattern 7: Sliding Window + HashSet (Uniqueness)

When to recognize it

  • No duplicates allowed

  • Frequency doesn’t matter, only presence

Keywords

  • “without repeating”

  • “unique characters”

Example

  • Longest Substring Without Repeating Characters

🧩 Pattern 8: Sliding Window with Prefix Trick (Advanced)

When to recognize it

  • Window sum doesn’t behave monotonically

  • Negative numbers involved

Keywords

  • “subarray sum equals k”

  • negative values present

Strategy

  • Prefix sum + hashmap

  • NOT classical sliding window