Skip to content

Commit ef1c977

Browse files
committed
feat(soobing): week7 > longest-substring-without-repeating-characters
1 parent ece6e47 commit ef1c977

File tree

1 file changed

+28
-0
lines changed
  • longest-substring-without-repeating-characters

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
ย (0)