Skip to content

Commit 85fb4cf

Browse files
committed
#243 solution
1 parent 563bc02 commit 85fb4cf

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
ํ’€์ด :
3+
์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์ด
4+
right๊ฐ€ ํ˜„์žฌ๊นŒ์ง€ ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ž๋ฉด right์˜ ๋ฌธ์ž๋ฅผ set์— ์ถ”๊ฐ€ ํ›„ right ์ฆ๊ฐ€, ๋ฌธ์ž์—ด๊ธธ์ด๋ฅผ ๋Š˜๋ฆฌ๊ณ  ์ตœ๋Œ€๊ธธ์ด์™€ ๋น„๊ตํ•ด์„œ ์—…๋ฐ์ดํŠธ
5+
์ด๋ฏธ ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜๋Š” ๋ฌธ์ž๋ฉด left์˜ ๋ฌธ์ž๋ฅผ set์—์„œ ์ œ๊ฑฐ ํ›„ left ์ฆ๊ฐ€
6+
7+
๋ฌธ์ž์—ด ๊ธธ์ด : N
8+
9+
TC : O(N)
10+
๋ฌธ์ž์—ด ์ „์ฒด์— ๋Œ€ํ•ด 1ํšŒ ์ˆœํšŒ
11+
12+
SC : O(N)
13+
ํ•ด์‹œํ…Œ์ด๋ธ” unordered_set์˜ ํฌ๊ธฐ๋Š” ๋ฌธ์ž์—ด ๊ธธ์ด์— ๋น„๋ก€๋ก€
14+
*/
15+
16+
#include <unordered_set>
17+
18+
class Solution {
19+
public:
20+
int lengthOfLongestSubstring(string s) {
21+
int ans = 0;
22+
int left = 0, right = 0;
23+
unordered_set<char> lookup;
24+
25+
while (right < s.size())
26+
{
27+
if (lookup.find(s[right]) == lookup.end()) {
28+
ans = max(ans, right - left + 1);
29+
lookup.insert(s[right]);
30+
right++;
31+
}
32+
else {
33+
lookup.erase(s[left]);
34+
left++;
35+
}
36+
}
37+
38+
return ans;
39+
}
40+
};

0 commit comments

Comments
ย (0)