Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
[v2] enumerate, try line.index, max(..., key=len) solution in Clear category for Long Non Repeat by Phil15
def non_repeat(line):
""" The longest substring without repeating chars. """
# Search the first repeating char.
for i1, ch in enumerate(line):
# We search the first two indexes (i1 and i2) of ch in the line.
try:
i2 = line.index(ch, i1 + 1)
except ValueError:
continue
# The solution is before i2 or strictly after i1.
return max(non_repeat(line[:i2]), non_repeat(line[i1 + 1:]), key=len)
return line # No repeating char here so it's the entire line.
March 27, 2019
Comments: