题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
枚举每个位置,往两边扩散,分别考虑回文串长度是奇数和偶数的情况,最后取 即可。
originalStr = str(input()) maxLen = 0 for i in range(len(originalStr) - 1): if originalStr[i] == originalStr[i + 1]: # 偶数长度回文子串 start = i end = i + 1 while start >= 0 and end < len(originalStr) and originalStr[start] == originalStr[end]: start -= 1 end += 1 maxLen = max(maxLen, end - start - 1) if originalStr[i - 1] == originalStr[i + 1]: # 奇数长度回文子串 start = i - 1 end = i + 1 while start >= 0 and end < len(originalStr) and originalStr[start] == originalStr[end]: start -= 1 end += 1 maxLen = max(maxLen, end - start - 1) print(maxLen)