This page looks best with JavaScript enabled

Algo Review: sliding window

 ·  ☕ 1 min read · 👀... views

A review on basic operation of sliding window.

Base template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def sliding_window(s: str):
    window = ... # int/map/etc.

    left, right = 0, 0
    while right < len(s):
        # enlarge window
        window.add(s[right])
        right += 1

        while left. < right and window.need_shrink():
            window.remove(s[left])
            left += 1
            # update result
Share on
Support the author with