题解 | 密码截取

密码截取

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)

全部评论

相关推荐

点赞 评论 收藏
分享
嵌入式求职之路:可以看我经验😂,https://www.nowcoder.com/share/jump/73221730841876945
点赞 评论 收藏
分享
雪飒:我也遇见过,我反问他有考虑来华为od吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务