longest palindrome substring
Anonymous User
68

Can someone please explain this code logic : why we are comparing index with r?

class Solution:
    def longestPalindrome(self, s: str) -> str:
        if not s or len(s) == 1 or (len(s) == 2  and s == s[::-1]):
            return s
        
        def isPalindrome(left, right):
            return s[left:right] == s[left:right][::-1]

        left, right = 0, 1
        for index in range(1, len(s)):
            if index > right and isPalindrome(index - right - 1, index + 1):
                left, right = index - right - 1, right + 2
            if index >= right and isPalindrome(index - right, index + 1):
                left, right = index - right, right + 1
                
                
        return s[left: left + right]

Comments (0)