Return the length of the longest substring of s that contains no repeated characters.
Remember the last index of each character. On a repeat, jump lo straight past the previous occurrence instead of removing characters one by one.
1function lengthOfLongestSubstring(s: string): number {2const last = new Map<string, number>();3let lo = 0, best = 0;4for (let hi = 0; hi < s.length; hi++) {5if (last.has(s[hi]) && last.get(s[hi])! >= lo) lo = last.get(s[hi])! + 1;6last.set(s[hi], hi);7best = Math.max(best, hi - lo + 1);8}9return best;10}
Empty map.
Space: play/pause · ←/→: step