Skip to content

Commit 6b21279

Browse files
committed
longest-substring-without-repeating-characters sol (py)
1 parent 817a5fe commit 6b21279

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

β€Žlongest-substring-without-repeating-characters/hi-rachel.pyβ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,29 @@ def lengthOfLongestSubstring(self, s: str) -> int:
5858
max_len = max(max_len, right - left + 1)
5959

6060
return max_len
61+
62+
63+
# HashMap 풀이
64+
def lengthOfLongestSubstring(s: str) -> int:
65+
if not s:
66+
return 0
67+
68+
left = 0 # μœˆλ„μš° μ‹œμž‘μ 
69+
max_length = 0 # μ΅œλŒ€ 길이
70+
seen = {} # 문자의 λ§ˆμ§€λ§‰ λ“±μž₯ μœ„μΉ˜λ₯Ό μ €μž₯ν•˜λŠ” ν•΄μ‹œλ§΅
71+
72+
for right in range(len(s)):
73+
char = s[right]
74+
75+
# ν˜„μž¬ λ¬Έμžκ°€ μœˆλ„μš° 내에 이미 μ‘΄μž¬ν•˜λŠ” 경우
76+
if char in seen and seen[char] >= left:
77+
# μœˆλ„μš° μ‹œμž‘μ μ„ 쀑볡 문자 λ‹€μŒ μœ„μΉ˜λ‘œ 이동
78+
left = seen[char] + 1
79+
80+
# ν˜„μž¬ 문자의 μœ„μΉ˜ μ—…λ°μ΄νŠΈ
81+
seen[char] = right
82+
83+
# ν˜„μž¬ μœˆλ„μš° 길이와 μ΅œλŒ€ 길이 비ꡐ ν›„ μ—…λ°μ΄νŠΈ
84+
max_length = max(max_length, right - left + 1)
85+
86+
return max_length

0 commit comments

Comments
Β (0)