File tree Expand file tree Collapse file tree 3 files changed +25
-2
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 3 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -20,4 +20,4 @@ var lengthOfLongestSubstring = function(s) {
2020 }
2121
2222 return maxLength ;
23- } ;
23+ } ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change @@ -21,4 +21,4 @@ function reverseList(head) {
2121 }
2222
2323 return prev ; // prev는 새로운 head
24- }
24+ }
You can’t perform that action at this time.
0 commit comments