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 11class 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
2120if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments