← Sliding Window

Micro-Drill #105 — Fixed-size window with Counter

Sliding Window Target: 15s

Counter-based fixed window slides character frequencies. Powers anagram detection.

from collections import Counter
w = Counter(s[:k])
for i in range(k, len(s)):
    w[s[i]] += 1
    w[s[i-k]] -= 1
    if w[s[i-k]] == 0: del w[s[i-k]]

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #104 Micro #106 →