Skip to content

Commit da4df4f

Browse files
author
Jinbeom
committed
Longest Substring Without Repeating Characters Solution
1 parent 4f4ad89 commit da4df4f

File tree

1 file changed

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

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
# ์‹œ๊ฐ„๋ณต์žก๋„: O(N)
3+
# ๊ณต๊ฐ„๋ณต์žก๋„: O(N)
4+
# set์—์„œ ์ œ์™ธํ•˜๋Š” ๋กœ์ง์„ ์—†์• ๊ธฐ ์œ„ํ•ด, map์„ ์‚ฌ์šฉํ•ด์„œ idx๊ฐ’์„ ์ €์žฅํ›„, start์™€ ๋น„๊ตํ•ด์„œ start๋ณด๋‹ค ์ž‘์€ idx๋ฅผ ๊ฐ€์ง„ ๊ฒฝ์šฐ์—๋Š” ์ค‘๋ณต์ด ์•„๋‹ˆ๋ผ๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค.
5+
def lengthOfLongestSubstring(self, s: str) -> int:
6+
7+
last_idx = {}
8+
answer = 0
9+
start = 0
10+
11+
for idx, ch in enumerate(s):
12+
# ์ค‘๋ณต ์กฐํšŒ์‹œ idx๊ฐ’๊ณผ start ๊ฐ’ ๋น„๊ต๋ฅผ ํ†ตํ•ด, ์‚ญ์ œํ•˜๋Š” ๋กœ์ง์„ ์—†์ด ์ค‘๋ณต์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.
13+
if ch in last_idx and last_idx[ch] >= start:
14+
start = last_idx[ch] + 1
15+
last_idx[ch] = idx
16+
else:
17+
answer = max(answer, idx - start + 1)
18+
last_idx[ch] = idx
19+
20+
return answer

0 commit comments

Comments
ย (0)