Why it works
- Any piece without repeats that ends at right can start no earlier than one past the last copy of s[right]. So left jumps straight there instead of stepping one letter at a time.
- The map is never cleaned, so it may hold a letter that has already left the window. The check last[c] ≥ left tells the two apart: a copy before left is no repeat, and left never moves back.
- The window has no repeats after every step, and each right is measured once, so the longest such piece is the best of these lengths.
Complexity
Time O(n): one pass, map lookups are O(1), and left only jumps forward. Memory O(k), where k is the number of different letters.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗