题解 | #最长回文子串#

最长回文子串

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))









全部评论

相关推荐

04-25 19:29
已编辑
宁波大学 运营
被普调的六边形战士很高大:你我美牛孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务