Skip to content

Longest Palindromic Substring

5. Longest Palindromic Substring

class Solution:
    def longestPalindrome(self, s: str) -> str:
        def getLen(s, l, r):
            while l >= 0 and r < len(s) and s[l] == s[r]:
                l -= 1
                r += 1
            return r - l - 1

        maxLen = 0
        start = 0
        for i in range(len(s)):
            curMax = max(getLen(s, i, i), getLen(s, i, i + 1))
            if curMax <= maxLen:
                continue
            maxLen = curMax
            start = i - (curMax - 1) // 2
        return s[start : start + maxLen]

Reference:

  1. Blind Curated 75
  2. 花花酱 LeetCode 5. Longest Palindromic Substring - 刷题找工作 EP292

Comments