9/32
Part 3 of 6 · A growing windowLearn and do

The window grows on the right and shrinks while it holds a repeat

The window keeps that work. right moves forward one entry per step and adds it. If the new entry breaks the rule, left moves forward, removing entries, until the window is valid again.

In code, a set seen holds the keys in keys[left:right + 1], and left starts at 0. For each right and its key, a while key in seen loop removes keys[left] from the set and adds 1 to left. When that loop ends, the key isn't in the window any more, so add it to seen. The window now runs from left to right, and its length is right - left + 1. The longest of those lengths over the pass is the answer, 5 for this log.

Predict

The window is u3 u1 u9 u7 u2, and the u1 at index 7 comes in. Which keys does left drop to make the window valid?

Pick an answer above to see the rest