Clean and easy to understand solution (Python)


  • 0
    F
    class Solution(object):
        def longestPalindrome(self, s):
            """
            :type s: str
            :rtype: str
            """
            result = ''
            if s is None:
            	return result
            for i in range(len(s)):
                max_sub_pal_this = self.max_palin(s, i, i)
                max_sub_pal_that = self.max_palin(s, i, i+1)
                if len(result) < len(max_sub_pal_this):
                    result = max_sub_pal_this
                if len(result) < len(max_sub_pal_that):
                    result = max_sub_pal_that
            return result
        
        def max_palin(self, s, ind1, ind2):
            low, high = None, None
            while ind1 >=0 and ind2 < len(s) and s[ind1] == s[ind2]:
                low, high = ind1, ind2
                ind1 = ind1-1
                ind2 = ind2+1
            return s[low:high+1] if low is not None and high is not None else ""
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.