题解 | 密码截取
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
# 拓展法 s = input() def find_substring(chars): substring = [] # 存储回文子串 # 齐数回文子串 for i in range(len(chars)): left = i right = i while left >= 0 and right < len(chars) and chars[left] == chars[right]: substring.append(chars[left : right + 1]) # 存储满足回文子串条件的子串 left -= 1 # 向外拓展 right += 1 # 偶数回文子串 for j in range(len(chars)): left = j right = j + 1 while left >= 0 and right < len(chars) and chars[left] == chars[right]: substring.append(chars[left : right + 1]) # 存储满足回文子串条件的子串 left -= 1 right += 1 return set(substring) sub = find_substring(s) max_len = max([len(i) for i in sub]) print(max_len)