Problem Statement
Which task is a classic fit for the sliding window pattern?
Explanation
Sliding window keeps a moving range and updates state as the window grows or shrinks. It shines for subarray or substring problems with local constraints.
Graph algorithms and matrix math do not fit this pattern well.
Code Solution
SolutionRead Only
def longest_unique(s):
seen, left, best = {}, 0, 0
for right, ch in enumerate(s):
if ch in seen and seen[ch] >= left:
left = seen[ch] + 1
seen[ch] = right
best = max(best, right-left+1)
return best