File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ๋ฌธ์ ์ค๋ช
3
+ * - ์ค๋ณต๋์ง ์๋ ๊ฐ์ฅ ๊ธด ๋ถ๋ถ ๋ฌธ์์ด์ ๊ธธ์ด ๊ตฌํ๊ธฐ
4
+ *
5
+ * ์์ด๋์ด
6
+ * 1) Brute Force - O(n^2)
7
+ * 2) Sliding Window + Hash Set - O(n)
8
+ * - ํฌ ํฌ์ธํฐ๋ฅผ ํ์ฉํ์ฌ ์์์ ๊ณผ, ๋์ ๊ฐ๋ฅดํค๋ ๋๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉ.
9
+ * - ๋ ํฌ์ธํฐ๊ฐ ์ด๋ฏธ ์กด์ฌํ๋ ๋ฌธ์์ด์ ํฌํจํ๋ ๊ฒฝ์ฐ ์์ ํฌ์ธํฐ๋ฅผ ๋ ํฌ์ธํฐ๊ฐ ์กด์ฌํ์ง ์๋ ๊ณณ๊น์ง ์ด๋์ํจ๋ค.
10
+ *
11
+ */
12
+ function lengthOfLongestSubstring ( s : string ) : number {
13
+ const seen = new Set < string > ( ) ;
14
+ let left = 0 ;
15
+ let maxLength = 0 ;
16
+
17
+ for ( let right = 0 ; right < s . length ; right ++ ) {
18
+ while ( seen . has ( s [ right ] ) ) {
19
+ seen . delete ( s [ left ] ) ;
20
+ left ++ ;
21
+ }
22
+
23
+ seen . add ( s [ right ] ) ;
24
+ maxLength = Math . max ( maxLength , right - left + 1 ) ;
25
+ }
26
+
27
+ return maxLength ;
28
+ }
You canโt perform that action at this time.
0 commit comments