# 假设 s = "abcabc"
# 无重复子串有 abc,bac,cab,ca,ab,a,b,c
# 最大无重复子串长度为 3
# 字节测开会要求你在算法走通的基础上,为你写的算法增加几个边界case
s1 = "abcabcabcb"
s2 = ["aaaaaaa", "", " ", " ", 'abab']
def cal(s):
s = list(s)
if len(s) == 0:
return 0
else:
p = 0
max_length = 1
for i in range(1, len(s)):
if s[i] not in s[p:i]:
max_length = i - p + 1
else:
p += 1
return max_length
for s in s2:
print(f"字符串:{s} 的最大无重复字串长度为:{cal(s)}")
字节算法题-最大无重复子串的长度
于 2023-08-16 15:03:48 首次发布