leecode 5. Longest Palindromic Substring最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example 2:
Input: “cbbd”
Output: “bb”
class Solution:
def longestPalindrome(self, s: str) -> str:
r=''
for i,j in [(i,j) for i in range(len(s)) for j in (0,1)]:
while i>-1 and i+j<len(s) and s[i]==s[i+j]:
i,j=i-1,j+2
r=max(r,s[i+1:i+j],key=len)
return '' if not s else r