Skip to content

Commit c632276

Browse files
committed
solve logest-substring-without-repeating-characters
1 parent fe403cd commit c632276

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

  • longest-substring-without-repeating-characters
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function lengthOfLongestSubstring(s: string): number {
2+
if (s.length <= 1) return s.length;
3+
4+
let left = 0;
5+
let right = 0;
6+
let maxLen = -1;
7+
const sMap = new Map();
8+
while (right !== s.length) {
9+
const curS = s[right];
10+
// Map에 존재하지 않는다 -> 중복 X -> right 전진
11+
if (!sMap.get(curS)) {
12+
sMap.set(s[right], 1);
13+
right++;
14+
} else {
15+
// Map에 존재 한다 -> 중복 O -> left 전진
16+
while (left <= right && sMap.get(curS)) {
17+
sMap.set(s[left], sMap.get(s[left]) - 1);
18+
left++;
19+
}
20+
}
21+
maxLen = Math.max(maxLen, right - left);
22+
}
23+
return maxLen;
24+
}

0 commit comments

Comments
 (0)