题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
# left-> <-right # 'abba' def isPalindrome(s): left = 0 right = len(s) - 1 while left < right: if s[left] != s[right]: return False left += 1 right -= 1 return True # print(isPalindrome('aba')) # print(isPalindrome('abc')) # print(isPalindrome('abba')) s = input() len_s = len(s) max_substring = "" # i : 字符串左闭包 # j + 1: 字符串右开包 for i in range(0, len_s, 1): for j in range(i + 1, len_s, 1): if isPalindrome(s[i : j + 1]) and len(s[i : j + 1]) > len(max_substring): max_substring = s[i : j + 1] # print(max_substring) print(len(max_substring))