Skip to content

Commit 12d378d

Browse files
committed
logest-substring-without-repating-characters solution
1 parent d8dfa07 commit 12d378d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function(s) {
6+
let start = 0;
7+
let maxLength = 0;
8+
const seen = new Map(); // 문자 -> 마지막 인덱스
9+
10+
for (let end = 0; end < s.length; end++) {
11+
const char = s[end];
12+
13+
// 중복 문자가 이전에 등장했으면 start를 갱신
14+
if (seen.has(char) && seen.get(char) >= start) {
15+
start = seen.get(char) + 1;
16+
}
17+
18+
seen.set(char, end); // 현재 문자 위치 갱신
19+
maxLength = Math.max(maxLength, end - start + 1);
20+
}
21+
22+
return maxLength;
23+
};

0 commit comments

Comments
 (0)