Skip to content

Commit eadd4eb

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents a4b1982 + 2496829 commit eadd4eb

File tree

138 files changed

+5411
-129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+5411
-129
lines changed

โ€Ždesign-add-and-search-words-data-structure/Jay-Mo-99.py

Lines changed: 0 additions & 121 deletions
This file was deleted.

โ€Žlongest-increasing-subsequence/forest000014.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
# solution
1212
์ด ๋ฌธ์ œ๋Š” DP๋กœ ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค.
1313
14-
LIS(1, x)๋ฅผ ๋ฒ”์œ„ [1, x] ๋‚ด์˜ LIS(๋‹จ, nums[x]๋ฅผ ๋ฐ˜๋“œ์‹œ ํฌํ•จ)์˜ ๊ธธ์ด๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. - (1)
15-
1 <= j < i ์ธ ๋ชจ๋“  j์— ํ•œ LIS(1, j)๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด, LIS(1, i)๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
16-
LIS(1, i) = max(LIS(1, j)) (๋‹จ, j๋Š” 1 <= j < i ์ด๊ณ , nums[j] < nums[i]) - (2)
14+
LIS(0, x)๋ฅผ ๋ฒ”์œ„ [0, x] ๋‚ด์˜ LIS(๋‹จ, nums[x]๋ฅผ ๋ฐ˜๋“œ์‹œ ํฌํ•จ)์˜ ๊ธธ์ด๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. - (1)
15+
1 <= j < i ์ธ ๋ชจ๋“  j์— ํ•œ LIS(1, j)๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด, LIS(0, i)๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
16+
LIS(0, i) = max(LIS(0, j)) (๋‹จ, j๋Š” 0 <= j < i ์ด๊ณ , nums[j] < nums[i]) - (2)
1717
18-
max(LIS(1, j))๋ฅผ ๊ตฌํ•  ๋•Œ, ๋ชจ๋“  j์— ๋Œ€ํ•ด ํƒ์ƒ‰ํ•œ๋‹ค๋ฉด, ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n^2)๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ผ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
18+
max(LIS(0, j))๋ฅผ ๊ตฌํ•  ๋•Œ, ๋ชจ๋“  j์— ๋Œ€ํ•ด ํƒ์ƒ‰ํ•œ๋‹ค๋ฉด, ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n^2)๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ผ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
1919
์ด ํƒ์ƒ‰ ๊ณผ์ •์„ ์ค„์ด๊ธฐ ์œ„ํ•ด, ์•„๋ž˜์˜ ์‚ฌ๊ณ  ๊ณผ์ •์„ ๊ฑฐ์ณค์Šต๋‹ˆ๋‹ค.
2020
2121
์–ด๋–ค ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ O(logn) ์‹œ๊ฐ„์— ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ(ํ˜น์€ ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
22-
(์ด ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์˜ x๋ฒˆ์งธ leaf ๋…ธ๋“œ์—๋Š” LIS(1, x) ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , internal ๋…ธ๋“œ์—๋Š” ์ž์‹ ๋…ธ๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.)
22+
(์ด ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์˜ x๋ฒˆ์งธ leaf ๋…ธ๋“œ์—๋Š” LIS(0, x) ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , internal ๋…ธ๋“œ์—๋Š” ์ž์‹ ๋…ธ๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.)
2323
2424
๋‹ค๋งŒ, ๋‹จ์ˆœํžˆ ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ๋ถ€์กฑํ•˜๊ณ , nums[j] < nums[i]์ธ j๋งŒ์„ ํ›„๋ณด๋กœ ์‚ผ์•„์•ผ ํ•  ํ…๋ฐ์š”,
25-
๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์— ๋ชจ๋“  leaf ๋…ธ๋“œ๋ฅผ ๋ฏธ๋ฆฌ ์‚ฝ์ž…ํ•ด๋‘๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆœ์ฐจ์ ์œผ๋กœ max(LIS(1, i))์˜ ๊ณ„์‚ฐ๊ณผ ์‚ฝ์ž…์„ ๋ฒˆ๊ฐˆ์•„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
26-
nums[i]์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒƒ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ, "max(LIS(1, j))๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , leaf๋ฅผ ํ•˜๋‚˜ ์‚ฝ์ž…"ํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
27-
nums[i]๋ณด๋‹ค ๋” ํฐ ๊ฐ’์€ ์•„์ง ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์— ์‚ฝ์ž…๋˜์ง€ ์•Š์€ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์—์„œ ๊ตฌ๊ฐ„ [1, i-1]์˜ ์ตœ๋Œ€๊ฐ’์„ ์กฐํšŒํ•˜๋ฉด nums[j] < num[i]์ธ j์— ๋Œ€ํ•ด์„œ๋งŒ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๊ฒŒ ๋˜๋ฏ€๋กœ,
25+
๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์— ๋ชจ๋“  leaf ๋…ธ๋“œ๋ฅผ ๋ฏธ๋ฆฌ ์‚ฝ์ž…ํ•ด๋‘๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆœ์ฐจ์ ์œผ๋กœ max(LIS(0, i))์˜ ๊ณ„์‚ฐ๊ณผ ์‚ฝ์ž…์„ ๋ฒˆ๊ฐˆ์•„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
26+
nums[i]์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒƒ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ, "max(LIS(0, j))๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , leaf๋ฅผ ํ•˜๋‚˜ ์‚ฝ์ž…"ํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
27+
nums[i]๋ณด๋‹ค ๋” ํฐ ๊ฐ’์€ ์•„์ง ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์— ์‚ฝ์ž…๋˜์ง€ ์•Š์€ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์—์„œ ๊ตฌ๊ฐ„ [0, i-1]์˜ ์ตœ๋Œ€๊ฐ’์„ ์กฐํšŒํ•˜๋ฉด nums[j] < num[i]์ธ j์— ๋Œ€ํ•ด์„œ๋งŒ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๊ฒŒ ๋˜๋ฏ€๋กœ,
2828
(2)๋ฒˆ ๊ณผ์ •์„ O(logn) ์‹œ๊ฐ„์— ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
2929
๋”ฐ๋ผ์„œ ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(nlogn)์ด ๋ฉ๋‹ˆ๋‹ค.
3030
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode_study
2+
3+
/*
4+
* ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์€ ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
5+
* ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋ฐ”๊นฅ ๋ฐ˜๋ณต๋ฌธ์—์„  ์‹œ์ž‘ ๋ฌธ์ž๋ฅผ ๋‚ด๋ถ€ ๋ฐ˜๋ณต๋ฌธ์—์„  ๊ทธ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ค‘๋ณต๋œ ๋ฌธ์ž๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ์„ ๋•Œ, ๋ฐ˜๋ณต๋ฌธ ํƒˆ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ
6+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^2)
7+
* -> ์ด์ค‘ ๋ฐ˜๋ณต ๊ณผ์ •
8+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
9+
* -> ์ค‘๋ณต๋˜์ง€ ์•Š์€ ๋ฌธ์ž์—ด์„ ๋‹ด์„ ๊ณต๊ฐ„ ํ•„์š”
10+
* */
11+
fun lengthOfLongestSubstring(s: String): Int {
12+
var maximumLength = 0
13+
for (start in s.indices) {
14+
val tempList = mutableListOf<Char>()
15+
for (end in start until s.length) {
16+
if (tempList.contains(s[end])) {
17+
break // escape loop
18+
}
19+
tempList.add(s[end])
20+
maximumLength = max(tempList.size, maximumLength)
21+
}
22+
}
23+
return maximumLength
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
๏ปฟ๏ปฟ #ํ•ด์„
2+
#
3+
4+
5+
#Big O
6+
#- N: str s์˜ ๊ธธ์ด
7+
8+
#Time Complexity: O(N)
9+
#- start,end: ๊ฐ๊ฐ ์ตœ๋Œ€ N๋ฒˆ ์›€์ง์ž„ -> O(N)
10+
#- set์˜ ์‚ฝ์ž…, ์‚ญ์ œ -> O(1)
11+
12+
#Space Complexity: O(N)
13+
#- chars: ์ตœ์•…์˜ ๊ฒฝ์šฐ chars๋Š” s์˜ ๋ชจ๋“  char์„ ์ €์žฅํ•œ๋‹ค -> O(N)
14+
15+
class Solution:
16+
def lengthOfLongestSubstring(self, s: str) -> int:
17+
max_len = 0
18+
chars = set() #ํ˜„ ์œˆ๋„์šฐ ๋‚ด ์ค‘๋ณต ์—†์ด ์กด์žฌํ•˜๋Š” ๋ฌธ์ž๋“ค์„ ์ €์žฅํ•˜๋Š” set
19+
start, end = 0, 0 # ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์˜ start ์ธ๋ฑ์Šค, end ์ธ๋ฑ์Šค
20+
#end๊ฐ€ s์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ๋„๋‹ฌํ• ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
21+
while end < len(s):
22+
#s[end]๊ฐ€ chars์— ์กด์žฌํ•˜๋ฉด
23+
if s[end] in chars:
24+
#chars์˜ s[start]๋ฅผ ์ œ๊ฑฐ
25+
chars.remove(s[start])
26+
#start๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™, ์œˆ๋„์šฐ ์ถ•์†Œ
27+
start += 1
28+
else: #s[end] ๊ฐ€ chars์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด
29+
chars.add(s[end]) #ํ•ด๋‹น s[end]๋ฅผ chars์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค
30+
end += 1 #end๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์˜ฎ๊ฒจ ์œˆ๋„์šฐ ํ™•์žฅ
31+
max_len = max(end - start, max_len) #start-end ๊ณ„์‚ฐํ•˜์—ฌ update
32+
return max_len
33+
34+
mySolution = Solution()
35+
mySolution.lengthOfLongestSubstring("pwwkew")
36+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
6+
// ๐ŸŒŸ sliding window technique
7+
8+
// Time Complexity: O(n)
9+
// Why it's O(n)
10+
// - The end pointer iterates over the string exactly once (O(n)).
11+
// - The start pointer also only moves forward without re-processing elements (O(n)).
12+
// - Therefore, the total amount of work done is proportional to the length of the string (n).
13+
// - So, even though there is a while loop inside the for loop, the total work done (number of operations) is still linear, O(n), because the start and end pointers together move across the string just once.
14+
// - This is the key reason why the time complexity is O(n), even with nested loops.
15+
16+
// Space Complexity: O(k), where k k is the length of the longest substring without repeating characters.
17+
// In the worst case, k = n, so O(n)
18+
19+
var lengthOfLongestSubstring = function (s) {
20+
let start = 0;
21+
let longest = 0;
22+
let subString = new Set();
23+
24+
for (let end = 0; end < s.length; end++) {
25+
while (subString.has(s[end])) {
26+
// Shrink the window by moving start
27+
subString.delete(s[start]);
28+
start += 1;
29+
}
30+
31+
subString.add(s[end]);
32+
longest = Math.max(longest, end - start + 1);
33+
}
34+
35+
return longest;
36+
};
37+
38+
// ๐Ÿ› ๏ธ Solution 1
39+
// Time Complexity: O(n^2), where n is the length of the string s
40+
// Space Complexity: O(k), where k is the length of the longest substring without repeating characters (k โ‰ค n)
41+
42+
// why the space complexity is not just O(n):
43+
// - Saying O(n) is technically correct in the worst case,
44+
// - but it hides the fact that the actual space usage is proportional to the length of the longest substring without repeats,
45+
// - which could be much smaller than n in many practical cases (e.g., for a string like "aaabbbccc").
46+
47+
// var lengthOfLongestSubstring = function (s) {
48+
// let longest = 0;
49+
50+
// for (let i = 0; i < s.length; i++) {
51+
// let subString = new Set();
52+
53+
// for (let j = i; j < s.length; j++) {
54+
// if (subString.has(s[j])) {
55+
// break;
56+
// } else {
57+
// subString.add(s[j]);
58+
// longest = Math.max(longest, j - i + 1);
59+
// }
60+
// }
61+
// }
62+
// return longest;
63+
// };
64+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Constraints:
3+
- 0 <= s.length <= 5 * 10^4
4+
- s consists of English letters, digits, symbols and spaces.
5+
6+
Time Complexity: O(n)
7+
- ๋ฌธ์ž์—ด์„ ํ•œ๋ฒˆ๋งŒ ์ˆœํšŒ
8+
9+
Space Complexity: O(n)
10+
- ๋”•์…”๋„ˆ๋ฆฌ์— ๋ฌธ์ž์™€ ์ธ๋ฑ์Šค ์ €์žฅ
11+
12+
ํ’€์ด ๋ฐฉ๋ฒ•:
13+
1. ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์™€ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ™œ์šฉ
14+
2. seen ๋”•์…”๋„ˆ๋ฆฌ์— ๊ฐ ๋ฌธ์ž์˜ ๋งˆ์ง€๋ง‰ ๋“ฑ์žฅ ์œ„์น˜๋ฅผ ์ €์žฅ
15+
3. ์ค‘๋ณต ๋ฌธ์ž๋ฅผ ๋งŒ๋‚˜๋ฉด ์œˆ๋„์šฐ์˜ ์‹œ์ž‘์ (current_start)์„ ์ค‘๋ณต ๋ฌธ์ž ๋‹ค์Œ ์œ„์น˜๋กœ ์ด๋™
16+
4. ๋งค ๋‹จ๊ณ„์—์„œ ํ˜„์žฌ ์œˆ๋„์šฐ์˜ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์ตœ๋Œ€ ๊ธธ์ด ๊ฐฑ์‹ 
17+
5. ์ตœ์ข…์ ์œผ๋กœ ๊ฐ€์žฅ ๊ธด ์ค‘๋ณต ์—†๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด ๋ฐ˜ํ™˜
18+
"""
19+
20+
class Solution:
21+
def lengthOfLongestSubstring(self, s: str) -> int:
22+
seen = {}
23+
current_start = 0
24+
max_length = 0
25+
26+
for i in range(len(s)):
27+
char = s[i]
28+
29+
if char in seen and seen[char] >= current_start:
30+
current_start = seen[char] + 1
31+
32+
seen[char] = i
33+
34+
current_length = i - current_start + 1
35+
max_length = max(current_length, max_length)
36+
37+
return max_length
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode_study
2+
3+
/**
4+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n) ?
5+
* -
6+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
7+
* - charAndIdx ์— ์ตœ๋Œ€ n๊ฐœ์˜ ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ ๋‹ด์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ O(n) ์˜ ๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
8+
* */
9+
10+
fun lengthOfLongestSubstring(s: String): Int {
11+
var point = 0
12+
val charAndIdx = HashMap<Char, Int>()
13+
var cnt = 0
14+
var maxCnt = 0
15+
16+
while (point < s.length) {
17+
val nowChar = s[point]
18+
if (nowChar !in charAndIdx.keys) {
19+
charAndIdx[nowChar] = point
20+
cnt += 1
21+
point += 1
22+
} else {
23+
val prevSameCharIdx = charAndIdx[nowChar]!!
24+
point = prevSameCharIdx + 1
25+
charAndIdx.clear()
26+
cnt = 0
27+
}
28+
29+
maxCnt = maxOf(cnt, maxCnt)
30+
}
31+
32+
return maxCnt
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* ์ค‘๋ณต๋œ ๋ฌธ์ž๊ฐ€ ์—†๋Š” ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๊ตฌํ•˜๊ธฐ
3+
* ๋‹ฌ๋ ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•ด์„ค์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค
4+
* ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋ฐฉ์‹ ์ ์šฉ
5+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
6+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
7+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
8+
* @param s
9+
*/
10+
function lengthOfLongestSubstring(s: string): number {
11+
const set = new Set<string>();
12+
let start = 0;
13+
let end = 0;
14+
let maxLength = 0;
15+
16+
while (end < s.length) {
17+
if (set.has(s[end])) {
18+
set.delete(s[start])
19+
start++
20+
} else {
21+
set.add(s[end])
22+
maxLength = Math.max(maxLength, set.size)
23+
end++
24+
}
25+
}
26+
return maxLength
27+
}
28+

0 commit comments

Comments
ย (0)