题解 | #密码截取#
密码截取
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)