Skip to content

Commit bc5da91

Browse files
committed
feat: dict을 사용한 구현
1 parent 5a99c68 commit bc5da91

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

  • longest-substring-without-repeating-characters

longest-substring-without-repeating-characters/gyeo-ri.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s: str) -> int:
3+
used_characters = dict()
4+
longest_length = 0
5+
start_idx = 0
6+
7+
for end_idx, c in enumerate(s):
8+
if c in used_characters:
9+
duplicated_idx = used_characters[c]
10+
if duplicated_idx >= start_idx:
11+
start_idx = duplicated_idx + 1
12+
13+
used_characters[c] = end_idx
14+
longest_length = max(longest_length, end_idx - start_idx + 1)
15+
16+
return longest_length
17+
18+
19+
"""
20+
# Set을 이용한 구현(Dict와 복잡도 상으로는 큰 차이 없음)
121
class Solution:
222
def lengthOfLongestSubstring(self, s: str) -> int:
323
used_characters = set()
@@ -10,10 +30,11 @@ def lengthOfLongestSubstring(self, s: str) -> int:
1030
start_idx += 1
1131
1232
used_characters.add(s[end_idx])
33+
# 속도는 if문보다 약간 느리지만 가독성 크게 개선
1334
longest_length = max(longest_length, end_idx - start_idx + 1)
1435
1536
return longest_length
16-
37+
"""
1738

1839
if __name__ == "__main__":
1940
solution = Solution()

0 commit comments

Comments
 (0)