题解 | #密码截取#

密码截取

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

# l and r start at the same idx of s, or r = l + 1
def max_l_expand(s: str, l: int, r: int):
    max_l = 0
    # as long as l and r is not exceed the idx and l and r has the same chr
    # s[l:r+1] is a palindrome; thus update the max_l
    while l >= 0 and r < len(s) and s[l] == s[r]:
        max_l = r - l + 1
        l -= 1
        r += 1
    return max_l


raw_info = input()
res = 0
for i in range(len(raw_info)):
    # test a palindrome from i as an odd str
    res = max(res, max_l_expand(raw_info, i, i))
    # test a palindrome from i and i+1 as an even str
    res = max(res, max_l_expand(raw_info, i, i + 1))
print(res)

全部评论

相关推荐

想回学校的华夫饼不愿...:全是ppt大赛
点赞 评论 收藏
分享
投递北京经纬恒润科技股份有限公司等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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