Implement it on LeetCode. An optimization: instead of a set, use a map storing the last index of each character. When you find a duplicate, jump left directly to position after the previous occurrence: text if s[right] in lastSeen and lastSeen[s[right]] >= left: left = lastSeen[s[right]] + 1 This avoids the inner while loop but both approaches are .
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/two-pointers-sliding-window/longest-substring-without-repeating-implementation
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████