def length_of_longest_substring(st):
max_length = 0
max_substring = ""
if not st:
return 0, ""
i, j = 0, 0
while j < len(st):
if st[j] not in st[i:j]:
j += 1
if j == len(st):
if max_length < j - i:
max_length = j - i
max_substring = st[i:j]
else:
if max_length < j - i:
max_length = j - i
max_substring = st[i:j]
i += 1
return max_length, max_substring
# 测试通过
str1 = ""
str2 = "a"
str3 = "ab"
str4 = "abc"
str5 = "abbcdefgdhf"
for st in [str1, str2, str3, str4, str5]:
print(length_of_longest_substring(st))
空间消耗太多