MemotivaLeetCode Patterns Flashcards: Sliding Window, Fixed Size, Dynamic Size, Substring

How does a dynamic-size sliding window work?

LeetCode Patterns Flashcards: Sliding Window, Fixed Size, Dynamic Size, Substring

Аудио-карточка · 0:31

Nortren·

How does a dynamic-size sliding window work?

0:31

A dynamic-size sliding window expands and contracts based on a condition. Move the right pointer to expand the window until a condition is violated, then move the left pointer to shrink the window until the condition is restored. At each valid state, update the result. For example, to find the longest substring without repeating characters, expand right to include new characters. When a duplicate is found, shrink from the left until the duplicate is removed. The left pointer never moves backward, so both pointers traverse the array at most once each, giving O of n total time.
neetcode.io