File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
2-NORMAL/Python/3-longest-unique-substring/proposed-solution Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 1+ # Challenge: Given a string, find the length of the longest substring without repeating characters.
2+ # Use sliding window techniques to optimize the solution's performance.
3+
4+ def length_of_longest_substring (s ):
5+ char_index_map = {}
6+ left = 0
7+ max_length = 0
8+ max_substring = ""
9+
10+ for right in range (len (s )):
11+ if s [right ] in char_index_map and char_index_map [s [right ]] >= left :
12+ left = char_index_map [s [right ]] + 1
13+ char_index_map [s [right ]] = right
14+ if right - left + 1 > max_length :
15+ max_length = right - left + 1
16+ max_substring = s [left :right + 1 ]
17+
18+ return max_length , max_substring
19+
20+ # Example usage
21+ if __name__ == "__main__" :
22+ test_string = "abcabcbb"
23+ length , substring = length_of_longest_substring (test_string )
24+ print (f"Length of longest substring without repeating characters: { length } " )
25+ print (f"Longest substring without repeating characters: { substring } " )
You can’t perform that action at this time.
0 commit comments