Longest Substring without Repeating Characters

To save some lines, it is safe to just update the max_len at each iteration. This doesn’t change the asymptotic complexity.

Although this algorithm runs in time, as the substring length is capped. It may look like a solution though.

max_len = 0
substr = ""
for c in s:
    index = substr.rfind(c)
    if index == -1:
        substr += c
    else:
        substr = substr[index + 1:] + c
    max_len = max(len(substr), max_len)
return max_len