Ben Zhao
Ben Zhao

Reputation: 61

Longest Palindrome Substring Missing Edge Cases

The following code works for leetcode #5 but misses the edge cases where there is no palindrome found in the string.

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        if len(s) == 1:
            return s
        
        if len(s) == 2:
            reverse = s[::-1]
            if reverse == s:
                return s
            else:
                return s[0]
            
        count = 0
        length = 0
        for i in range(0, len(s)):
            start = s[i]
            for j in range(i+1, len(s)+1):
                if j == len(s):
                    break
                if i == len(s) - 1:
                    return s[0]
                start += s[j]
                if s[i] == s[j]:
                    count += 1
                    reverse = start[::-1]  
                    if reverse == start:
                        if len(reverse) > length:
                            length = len(reverse)
                            pal = reverse 
                        if j == len(s) - 1:
                            start = None
                            break
                    else:
                        if j == len(s) - 1:
                            start = None
                            break
                            
        return pal

I am not sure how to proceed with this edge case. Please advise on how to continue.

Upvotes: 1

Views: 69

Answers (1)

Alexander
Alexander

Reputation: 17291

All strings that have at least one character contain a palindrome because a singular character is itself a palindrome.

The solution to your problem is to simply assign the first character of the string as the solution at the top of the function like this:

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        pal = s[0]  # add this
        if len(s) == 1:
            return s
        
        if len(s) == 2:
            reverse = s[::-1]
            if reverse == s:
                return s
            else:
                return s[0]
            
        count = 0
        length = 0
        for i in range(0, len(s)):
            start = s[i]
            for j in range(i+1, len(s)+1):
                if j == len(s):
                    break
                if i == len(s) - 1:
                    return s[0]
                start += s[j]
                if s[i] == s[j]:
                    count += 1
                    reverse = start[::-1]  
                    if reverse == start:
                        if len(reverse) > length:
                            length = len(reverse)
                            pal = reverse 
                        if j == len(s) - 1:
                            start = None
                            break
                    else:
                        if j == len(s) - 1:
                            start = None
                            break
                            
        return pal

That way if it doesn't find any solution it will return the first character as the solution instead.

Upvotes: 1

Related Questions