Pattern
Pattern
Pattern
Maintain a valid contiguous window while expanding and shrinking it.
Expand right, update state, shrink left while invalid, then record the answer.
left = 0
counts = {}
best = 0
for right, value in enumerate(items):
counts[value] = counts.get(value, 0) + 1
while not window_is_valid(counts):
outgoing = items[left]
counts[outgoing] -= 1
if counts[outgoing] == 0:
del counts[outgoing]
left += 1
best = max(best, right - left + 1)