LeetCodeDiary

A Diary for solving LeetCode problems

View on GitHub
class Solution:
    def longestPalindrome(self, s: str) -> str:
        n = len(s)
        max_length = 0
        for i in range(2*n-1):
            left = i//2
            right = left + i%2
            length = right - left + 1
            while left >= 0 and right < n and s[left] == s[right]:
                left -= 1
                right += 1
                length += 2
            if length > max_length:
                ans = s[left+1:right]
                max_length = length
        return ans

动态规划 中等

https://leetcode-cn.com/problems/longest-palindromic-substring/