Buckets:
| 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" |
Xet Storage Details
- Size:
- 1.16 kB
- Xet hash:
- 8f858a7faad0955737331d8ba7390e19598a5037f5dc67e01c5393331f61d449
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.