File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * TC: O(S)
3+ * right์ S๋งํผ ์ํ + left์ S๋งํผ ์ํ
4+ * (๊ฐ ์ํ์ ๊ณฑ์ด ์๋ ํฉ์ธ ์ด์ ๋ right ์ํ ๋์ left์ ์ต๋ ์ํ๊ฐ S์ด๊ธฐ ๋๋ฌธ์
๋๋ค.)
5+ *
6+ * SC: O(N)
7+ * usedCharacter์ S๋งํผ ๋ค์ด๊ฐ ์ ์์ต๋๋ค.
8+ *
9+ * S: s.length
10+ */
11+
12+ /**
13+ * @param {string } s
14+ * @return {number }
15+ */
16+ var lengthOfLongestSubstring = function ( s ) {
17+ // 1. ์ฌ์ฉ๋ ๋ฌธ์๋ฅผ ๊ธฐ๋กํ๊ธฐ ์ํ set
18+ const usedCharacter = new Set ( ) ;
19+
20+ // 2. ์ ๋ต ์ ์ถ์ ์ํ ๋ถ๋ถ๋ฌธ์์ด ์ต๋ ๊ธธ์ด
21+ let maxLength = 0 ;
22+
23+ // 3. ์ํ๋ฅผ ์ํ ํฌ์ธํฐ + ๊ฐ index์์ ์ต๋ ๋ฌธ์์ด๊ธธ์ด๋ฅผ ๊ตฌํ๊ธฐ ์ํ ๋ณ์
24+ let left = 0 ;
25+ let right = 0 ;
26+
27+ while ( left <= right && right < s . length ) {
28+ // 4. [right] ๋ฌธ์๊ฐ ์ฌ์ฉ๋์์ผ๋ฉด
29+ if ( usedCharacter . has ( s [ right ] ) ) {
30+ // 5. ์ฌ์ฉ๋ ๋ฌธ์๋ฅผ ๋ฐ๊ฒฌํ๊ธฐ ์ ๊น์ง left ์ด๋ (+ ์ฌ์ฉ๋ [left] ๋ฌธ์ ๊ธฐ๋ก ์ ๊ฑฐ)
31+ while ( s [ left ] !== s [ right ] ) {
32+ usedCharacter . delete ( s [ left ] ) ;
33+ left += 1 ;
34+ }
35+
36+ // 6. [right] ๋ฌธ์์ [left] ๋ฌธ์๊ฐ ๋์ผํ๋ฏ๋ก left๋ง ์ด๋
37+ left += 1 ;
38+ } else {
39+ // 7. [right] ๋ฌธ์๊ฐ ๋ฏธ์ฌ์ฉ๋์์ผ๋ฉด ๊ธฐ๋ก ์ถ๊ฐ
40+ usedCharacter . add ( s [ right ] ) ;
41+ }
42+
43+ // 8. ์ค๋ณต์๋ ๋ถ๋ถ๋ฌธ์์ด ์ต๋ ๊ธธ์ด ๊ฐฑ์
44+ maxLength = Math . max ( maxLength , right - left + 1 ) ;
45+
46+ // 9. ๋ค์ ๋ฌธ์๋ก ์ด๋
47+ right += 1 ;
48+ }
49+
50+ return maxLength ;
51+ } ;
You canโt perform that action at this time.
0 commit comments