3. Longest Substring Without Repeating Characters

202407111223
tags: #sliding-window

var lengthOfLongestSubstring = function(s) {
	let start = 0;
	let res = 0;
	const map = new Map();
	for (let end = 0; end < s.length; end++) {
		map.set(s[end], (map.get(s[end]) ?? 0) + 1);
		while (map.get(s[end]) > 1) {
			map.set(s[start], map.get(s[start]) - 1);
			start += 1;
		}
		res = Math.max(res, end - start + 1);
	}
	return res;
};

Reference

https://leetcode.com/problems/longest-substring-without-repeating-characters/