Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Single pass solution with dictionary solution in Speedy category for Long Non Repeat by brubru777
def non_repeat(line):
"""
the longest substring without repeating chars
"""
best_start = 0
best_length = 0
start = 0
char_positions = {}
for i, c in enumerate(line):
if c in char_positions and char_positions[c] >= start:
start = char_positions[c] + 1
char_positions[c] = i
length = i - start + 1
if length > best_length:
best_start = start
best_length = length
return line[best_start:best_start + best_length]
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert non_repeat('aaaaa') == 'a', "First"
assert non_repeat('abdjwawk') == 'abdjw', "Second"
assert non_repeat('abcabcffab') == 'abcf', "Third"
print('"Run" is good. How is "Check"?')
Oct. 17, 2017