Skip to content

Commit 2fc1dda

Browse files
committed
feat: 슬라이싱 윈도우를 활용한 개선
1 parent 77c571a commit 2fc1dda

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

  • longest-substring-without-repeating-characters

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
class Solution:
22
def lengthOfLongestSubstring(self, s: str) -> int:
3-
longest_substring: str = ""
4-
current_substring: str = ""
3+
used_characters = set()
4+
longest_length = 0
5+
start_idx = 0
56

6-
for c in s:
7-
if len(longest_substring) < len(current_substring):
8-
longest_substring = current_substring
7+
for end_idx in range(len(s)):
8+
while s[end_idx] in used_characters:
9+
used_characters.remove(s[start_idx])
10+
start_idx += 1
911

10-
if c not in set(current_substring):
11-
current_substring += c
12+
used_characters.add(s[end_idx])
13+
current_length = end_idx - start_idx + 1
14+
if current_length > longest_length:
15+
longest_length = current_length
1216

13-
else:
14-
current_substring = current_substring.split(c)[1] + c
15-
16-
if len(longest_substring) < len(current_substring):
17-
longest_substring = current_substring
18-
return len(longest_substring)
17+
return longest_length
1918

2019

2120
if __name__ == "__main__":

0 commit comments

Comments
 (0)