fbaquant / LeetCode

1 stars 0 forks source link

Longest Palindromic Substring #29

Open fbaquant opened 9 months ago

fbaquant commented 9 months ago

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

fbaquant commented 9 months ago
class Solution:
    def longestPalindrome(self, s: str) -> str:

        def get_palindrome(l, r):
            while l >= 0 and r <= len(s) - 1 and s[l] == s[r]:
                l -= 1
                r += 1
            return s[l+1:r]

        n = len(s)
        ans = ''
        for i in range(n):
            p1 = get_palindrome(i, i)
            p2 = get_palindrome(i, i+1)
            ans = max([ans, p1, p2], key=len)
        return ans