MCPcopy
hub / github.com/keon/algorithms / get_longest_non_repeat_v3

Function get_longest_non_repeat_v3

algorithms/array/longest_non_repeat.py:127–149  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

125
126
127def 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

Callers

nothing calls this directly

Calls 2

addMethod · 0.80
removeMethod · 0.45

Tested by

no test coverage detected