File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ *@link https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
3
+ *
4
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
5
+ * - ์ฌ๋ผ์ฐ๋ฉ ์๋์ฐ์ set ์ฌ์ฉํด์ ์ค๋ณต์๋ ๋ฌธ์์ด ํ์ธ
6
+ * - ์ค๋ณต ๋ฌธ์ ๋ฐ๊ฒฌํ๋ฉด ์๋์ฐ ์ถ์
7
+ *
8
+ * ์๊ฐ๋ณต์ก๋ : O(n)
9
+ * - ๊ฐ ๋ฌธ์ ์ํํ๋๊น
10
+ *
11
+ * ๊ณต๊ฐ๋ณต์ก๋ : O(n)
12
+ * - ์ค๋ณต ์๋ ๊ฒฝ์ฐ ์ต๋ n๊ฐ์ ๋ฌธ์ set์ ์ ์ฅ
13
+ */
14
+ function lengthOfLongestSubstring ( s : string ) : number {
15
+ let start = 0 ,
16
+ end = 0 ,
17
+ maxLength = 0 ;
18
+ const set = new Set < string > ( ) ;
19
+
20
+ while ( end < s . length ) {
21
+ const char = s [ end ] ;
22
+
23
+ // ์ค๋ณต์ด ์์ผ๋ฉด ์๋์ฐ ์ถ์
24
+ if ( set . has ( char ) ) {
25
+ set . delete ( s [ start ] ) ;
26
+ start ++ ;
27
+ } else {
28
+ // ์ค๋ณต ์์ผ๋ฉด set์ ๋ฌธ์ ์ถ๊ฐ, ์๋์ฐ ํ์ฅ
29
+ set . add ( char ) ;
30
+ maxLength = Math . max ( maxLength , end - start + 1 ) ;
31
+ end ++ ;
32
+ }
33
+ }
34
+
35
+ return maxLength ;
36
+ }
You canโt perform that action at this time.
0 commit comments