File tree Expand file tree Collapse file tree
longest-substring-without-repeating-characters Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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와 복잡도 상으로는 큰 차이 없음)
121class 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
1839if __name__ == "__main__" :
1940 solution = Solution ()
You can’t perform that action at this time.
0 commit comments