Leetcode Tag Search | Back

Sliding window

Category: /template

Template

Explaination Details Template Index
# for LC Problem 76
# window is [left, right) [close and open)
def sliding_window(s, t):
    """ s, t are iterable """
    need, window = {}, {}  # counters
    need = collections.Counter(t)
    # need = {A: 1, B: 1, C: 1}
    left = right = 0
    valid = 0
    while right < len(s):
        c = s[right]
        right += 1  # move right window
        # ... update window
        # e.g. window[c] += 1
        
        print(f"left={left}, right={right}")
        # check left window
        while window needs shrink:
            d = s[left]
            left += 1
            # ... update window
            # e.g. window[d] -= 1

LC: 76, 567, 438, 3

讨论

提示

  • 如果看不到讨论部分, 请暂时关掉adblock in Firefox/Chrome
  • 本网站使用Javascript实现评论功能, 此处外链对提高您的网站PR没有帮助. (潜台词: 请不要灌水, 谢谢)