Skip to content

Commit c498b66

Browse files
committed
longest substring without repeating characters solution
1 parent 737a907 commit c498b66

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function (s) {
6+
7+
let maxLength = 0;
8+
let startIndex = 0;
9+
let endIndex = 0;
10+
11+
let charSet = new Set();
12+
13+
// 슬라이딩 윈도우
14+
while (endIndex < s.length) {
15+
let currentChar = s[endIndex];
16+
if (charSet.has(currentChar)) {
17+
charSet.delete(s[startIndex]);
18+
startIndex++;
19+
} else {
20+
charSet.add(currentChar);
21+
maxLength = Math.max(maxLength, charSet.size);
22+
endIndex++;
23+
}
24+
}
25+
return maxLength;
26+
};
27+
28+
29+

0 commit comments

Comments
 (0)