Hints — p29 Longest Substring Without Repeating Characters

Read one at a time.


Hint 1 — Substring, not subsequence. Contiguous. "pwwkew""wke" (length 3), not "pwke".


Hint 2 — Variable sliding window. Two pointers left, right. Expand right one char at a time. When the invariant “all chars in s[left..right] are unique” breaks, shrink left until it holds again. Record best = max(best, right - left + 1) each step.


Hint 3 — How to shrink efficiently. Option A: keep a SET of chars in the window; on duplicate at s[right], advance left one step at a time (removing from set) until the duplicate is gone. O(N) amortized (each char enters/leaves at most once).

Option B: keep a MAP char → last_seen_index. On duplicate, jump left directly past the last occurrence: left = last_seen[c] + 1.


Hint 4 — The "abba" trap. With Option B, on "abba": at right=3, c='a', last_seen['a']=0 but left=2 (we already moved past the earlier 'b'). Jumping to left = 0+1 = 1 would BACKTRACK and re-include the 'b' at index 2 — breaking uniqueness. Always left = max(left, last_seen[c]+1), never bare assign.


Hint 5 — Edge cases checklist. Empty → 0. Single char → 1. All same "bbbbb" → 1. All distinct "abcdef" → 6. The tricky ones: "abba", "dvdf", "tmmzuxt". Test these.


If still stuck: solution.py has both Option A (set) and Option B (map). Read the set version first — it’s self-documenting (left only moves forward).