Given a string s, find the length of the longest substring without repeating characters.
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring,
"pwke" is a subsequence and not a substring.
Input: s = ""
Output: 0
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let start = 0;
let end = 0;
let longest = 0;
let set = new Set();
while (start < s.length && end < s.length) {
if(!set.has(s[end])) {
set.add(s[end]);
longest = Math.max(longest, end - start + 1);
end++;
} else {
set.delete(s[start])
start++;
}
}
return longest;
};
92 ms, faster than 94.88% of JavaScript online submissions for Longest Substring Without Repeating Characters.
43.1 MB, less than 79.71% of JavaScript online submissions for Longest Substring Without Repeating Characters.
hash set에 익숙하지 않았고, sliding window의 개념도 처음 접해서 처음에 조금 헤맸다. 그래도 다른 풀이를 베끼지 않고 스스로 풀어서 보람은 있다!! 🔥🔥🔥🔥🔥🔥