Find the longest substring without repeating characters using sliding window. Args: string: Input string to search. Returns: A tuple of (length, substring) for the longest non-repeating substring. Examples: >>> get_longest_non_repeat_v3("abcabcbb") (3,
(string: str)
| 125 | |
| 126 | |
| 127 | def get_longest_non_repeat_v3(string: str) -> tuple[int, str]: |
| 128 | """Find the longest substring without repeating characters using sliding window. |
| 129 | |
| 130 | Args: |
| 131 | string: Input string to search. |
| 132 | |
| 133 | Returns: |
| 134 | A tuple of (length, substring) for the longest non-repeating substring. |
| 135 | |
| 136 | Examples: |
| 137 | >>> get_longest_non_repeat_v3("abcabcbb") |
| 138 | (3, 'abc') |
| 139 | """ |
| 140 | longest_substring = "" |
| 141 | seen = set() |
| 142 | start_index = 0 |
| 143 | for i in range(len(string)): |
| 144 | while string[i] in seen: |
| 145 | seen.remove(string[start_index]) |
| 146 | start_index += 1 |
| 147 | seen.add(string[i]) |
| 148 | longest_substring = max(longest_substring, string[start_index : i + 1], key=len) |
| 149 | return len(longest_substring), longest_substring |