题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
# 扩展中心法 def find_substring(s): sub = set() # 定义一个结果集,用来接收所有的回文子串,注意这里用set类型别用list # 奇数回文子串 for i in range(len(s)): left = i right = i while left >= 0 and right < len(s) and s[left] == s[right]: # 满足回文子串条件的加入结果集中 sub.add(s[left : right + 1]) # 向外拓展 left -= 1 right += 1 # 偶数回文子串 for i in range(len(s)): left = i right = i + 1 while left >= 0 and right < len(s) and s[left] == s[right]: # 满足回文子串条件的加入结果集中 sub.add(s[left : right + 1]) # 向外拓展 left -= 1 right += 1 return sub if __name__ == '__main__': s = input() result = find_substring(s) max_sub = max(result,key=len) print(len(max_sub))