题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
- 中心拓展法来求解,O(n^2)
string = input()
# 中心拓展法
def expand(s, i, j):
ans = 0
while i >=0 and j < len(s) and s[i] == s[j]:
ans = j - i + 1
i = i - 1
j = j + 1
return ans
ans = 0
for i in range(len(string)):
ans = max(ans, expand(string, i, i))
ans = max(ans, expand(string, i, i+1))
print(ans

