def find_longest_substring(string): # Dictionary that maps characters (treated as keys) to their most recent index, # so we don't re-examine them later. This speeds up the process of checking if a character was added in the current window. seen_indices = {} longest_start, longest_end = 0, 0 for i, char in enumerate(string): # If this is a new start position (i.e., we're starting from an existing match and then see something new), save it to the dictionary. if string[longest_start:longest_end+1] == longest_match: seen_indices[char] = max(seen_indices.get(char, 0), i - len(longest_match)) # If this is a new character that hasn't been added before, start with an index of the current position. else: seen_indices[char] = i if i == longest_end: # This means we've reached the end of our match. longest_start += len(longest_match) + 1 longest_end += len(longest_match) return string[longest_start : longend] # Test it out! print(find_longest_substring("abcabcbb")) # Should print "abc"