File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 1번 풀이
2
+ function lengthOfLongestSubstring1 ( s : string ) : number {
3
+ let seen = new Set < string > ( ) ;
4
+ let start = 0 ;
5
+ let maxLength = 0 ;
6
+
7
+ for ( let end = 0 ; end < s . length ; end ++ ) {
8
+ const char = s [ end ] ;
9
+
10
+ // 중복된 문자가 나오면 Set에서 제거하면서 start를 앞으로 이동
11
+ while ( seen . has ( char ) ) {
12
+ seen . delete ( s [ start ] ) ;
13
+ start ++ ;
14
+ }
15
+
16
+ // 중복이 없으면 현재 윈도우 길이 갱신
17
+ seen . add ( char ) ;
18
+ maxLength = Math . max ( maxLength , end - start + 1 ) ;
19
+ }
20
+
21
+ return maxLength ;
22
+ }
23
+
24
+ // 2번 풀이
25
+ function lengthOfLongestSubstring2 ( s : string ) : number {
26
+ let substring = "" ;
27
+ let maxLength = 0 ;
28
+
29
+ for ( let i = 0 ; i < s . length ; i ++ ) {
30
+ const char = s [ i ] ;
31
+
32
+ // 중복 문자가 있다면, 그 문자 이후부터 잘라냄
33
+ if ( substring . includes ( char ) ) {
34
+ const index = substring . indexOf ( char ) ;
35
+ substring = substring . slice ( index + 1 ) ;
36
+ }
37
+
38
+ substring += char ;
39
+ maxLength = Math . max ( maxLength , substring . length ) ;
40
+ }
41
+
42
+ return maxLength ;
43
+ }
You can’t perform that action at this time.
0 commit comments