"""
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
示例 1:
输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。
示例 2:
输入: "cbbd"
输出: "bb"
"""
def longestPalindrome(s):
str_length = len(s)
max_length = 0
index_left = 0
index_right = 0
for i in range(str_length):
for j in range(i,str_length):
if i == 0:
if s[i:j + 1] == s[j::-1]:
if max_length < len(s[i:j + 1]):
max_length = len(s[i:j + 1])
index_left = i
index_right = j
if i >0:
if s[i:j+1] == s[j:i-1:-1]:
if max_length < len(s[i:j+1]):
max_length = len(s[i:j+1])
index_left = i
index_right = j
return s[index_left:index_right+1]
def longestPalindrome(s):
m = ''
for i in range(len(s)):
for j in range(len(s), i, -1):
if len(m) >= j-1:
break
elif s[i:j] == s[i:j][::-1]:
m = s[i:j]
break
return m
print(longestPalindrome('abade'))