题解 | #最长回文子串#

最长回文子串

https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507

string = input().strip()

max_len = 0

for i in range(0, len(string)):
    left = i - 1
    right = i + 1
    len_ABA = 1
    while left >=0 and right <= len(string) - 1:
        if string[left] == string[right]:
            len_ABA += 2
            left -= 1
            right += 1 
        else:
            break
    
    left = i
    right = i + 1
    len_ABBA = 0

    while left >= 0 and right <= len(string) - 1:
        if string[left] == string[right]:
            len_ABBA += 2
            left -= 1
            right += 1
        else:
            break
    max_len = max([max_len, len_ABA, len_ABBA])

print(max_len)

全部评论

相关推荐

za_chary:你这个简历写的太强了,建议直接投大厂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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