Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions longest-substring-without-repeating-characters/hi-rachel.py
Copy link
Contributor

@hu6r1s hu6r1s Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 2중 for문 썼는데 좋은 방법이 있네요. 좋은 공부가 되었습니다~

Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,29 @@ def lengthOfLongestSubstring(self, s: str) -> int:
max_len = max(max_len, right - left + 1)

return max_len


# HashMap 풀이
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def lengthOfLongestSubstring(s: str) -> int:
if not s:
return 0

left = 0 # 윈도우 시작점
max_length = 0 # 최대 길이
seen = {} # 문자의 마지막 등장 위치를 저장하는 해시맵

for right in range(len(s)):
char = s[right]

# 현재 문자가 윈도우 내에 이미 존재하는 경우
if char in seen and seen[char] >= left:
# 윈도우 시작점을 중복 문자 다음 위치로 이동
left = seen[char] + 1

# 현재 문자의 위치 업데이트
seen[char] = right

# 현재 윈도우 길이와 최대 길이 비교 후 업데이트
max_length = max(max_length, right - left + 1)

return max_length
Loading