/** * @param {string} s * @return {number} */ var lengthOfLongestSubstringTwoDistinct = function(s) { let n = s.length if(n < 3) return n let left = 0, right = 0, max = 2 let map = newMap() while(right < n){ if(map.size < 3) map.set(s[right],right++) if(map.size === 3){ let minKey,minValue = Infinity for(let [key,value] of map.entries()){ if(minValue > value){ minValue = value minKey = key } } map.delete(minKey) left = minValue + 1 } max = Math.max(max,right-left) } return max };