Python Test Case Bug?


  • 0
    O

    I've tested my code in Python on my computer, and a few test cases went through fine, including test case "abb". My program outputs correctly "bb".

    However, in the Online Judge, my program's answer apparently is "ccc". I double checked that I'm using the same program on OJ and on my own computer. I suspect there is a broken case in the OJ's test cases?

    Following is my code. If anyone could tell me how exactly my code could have changed anything from "abb" to "ccc", I think it would be a miracle.

    class Solution:
    # @return a string
    cache = {}
    
    def longestPalindrome(self, s):
        maxlen = 0
        maxstr = ''
        if not s:
            return None
        if len(s) == 1:
            return s
        if len(s) == 2:
            str1 = self.checkPalindrome(s, 0, 1, 0)
            if len(str1) > 1:
                return s
            else:
                return s[0]
        for i in range(1,len(s)-1):
            str1 = self.checkPalindrome(s, i-1, i, 0)
            str2= self.checkPalindrome(s, i, i, 0)
            str3 = self.checkPalindrome(s, i, i+1, 0)
            locmax = max(len(str1),len(str2),len(str3))
            if locmax > maxlen:
                maxlen = locmax
                if len(str1) == locmax:
                    maxstr = str1
                elif len(str2) == locmax:
                    maxstr = str2
                else:
                    maxstr = str3
        return maxstr
    
    
    def checkPalindrome(self, s, pos1, pos2, distance):
        if (pos1,pos2) in self.cache:
            return self.cache[(pos1,pos2)]
    
        index1 = pos1 - distance
        index2 = pos2 + distance
        if index1-index2 <= 1 and index2-index1 <= 1:
            if s[index1] == s[index2]:
                return self.checkPalindrome(s, pos1, pos2, distance+1)
            else:
                self.cache[(pos1,pos2)] = ''
                return ''
        if index1 < 0 or index2 > len(s)-1 or s[index1]!=s[index2]:
            self.cache[(pos1,pos2)] = s[index1+1:index2]
            return s[index1+1:index2]
        else:
            return self.checkPalindrome(s, pos1, pos2, distance+1)

Log in to reply
 

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