# algorithms # Medium (25.62%) # Total Accepted: 370.8K # Total Submissions: 1.4M # Testcase Example: '"babad"'
# 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 : deflongestPalindrome(self, s): """ :type s: str :rtype: str """ if s isNone: return''
length = len(s) if length < 2: return s
s = self.str_trans(s)
length = len(s) res = [1for i in range(length)] right = c = -1 for i in range(length): res[i] = min(res[2*c-i], right-i) if right > i else1 while i-res[i] > -1and i+res[i] < length: if s[i-res[i]] == s[i+res[i]]: res[i] += 1 else: break res[i] -= 1
近期评论