File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * [3] Longest Substring Without Repeating Characters
3+ */
4+
5+ function lengthOfLongestSubstring ( s : string ) : number {
6+ // 문자열의 문자가 1개 이하인 경우 해당 문자열이 최대 길이 substring
7+ if ( s . length <= 1 ) return s . length ;
8+
9+ let maxLength = 1 ; // 최소 substring 길이는 문자 하나 길이인 1
10+ const charSet = new Set ( ) ;
11+ let left = 0 ,
12+ right = left + 1 ;
13+
14+ charSet . add ( s [ left ] ) ;
15+ while ( left < right && right < s . length ) {
16+ // right의 문자가 charSet에 없을 때 까지 까지 left를 이동
17+ while ( charSet . has ( s [ right ] ) && left < right ) {
18+ charSet . delete ( s [ left ] ) ;
19+ left ++ ;
20+ }
21+ // substring 길이 업데이트하기
22+ maxLength = Math . max ( maxLength , right - left + 1 ) ;
23+ // right를 오른쪽으로 한칸 옮기기
24+ charSet . add ( s [ right ] ) ;
25+ right ++ ;
26+ }
27+
28+ return maxLength ;
29+ }
You can’t perform that action at this time.
0 commit comments