题解 | 密码截取

密码截取

https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1

#奇数和偶数回文后进入列表,找出列表中的最长回文长度即所求

s = input()
length = len(s)
huiwen = []
for i in range(length):
    right, left = i, i
    while left >= 0 and right < length and s[left] == s[right]:
        huiwen.append(s[left : right + 1])
        right += 1
        left -= 1

    right, left = i + 1, i
    while left >= 0 and right < length and s[left] == s[right]:
        huiwen.append(s[left : right + 1])
        right += 1
        left -= 1

max_num = 0

for i in range(len(huiwen)):
    max_num = max(max_num, len(huiwen[i]))
print(max_num)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务