最长的回文子串是哪一段
一个字符串 abcbdcba(8 个字符)。回文就是正着读和倒着读一样。
S = "abcbdcba"
def longest_pal_substr(s):
best = ""
for i in range(len(s)):
for j in range(i + 1, len(s) + 1):
w = s[i:j]
if w == w[::-1] and len(w) > len(best):
best = w
return best
print(longest_pal_substr(S))
全部评论