I am getting back on the Leetcode grind in preparation for rejoining the workforce as a SDE. I am pretty certain I got this question right, and according to my custom test case it does pass. Why does it not behave as expected when submitted?
<code>class Solution:
def longestPalindrome(self, s: str, checked = set()) -> str:
print(s, self.isPalindrome(s))
if len(s) == 1 or self.isPalindrome(s):
return s
if s in checked:
print(s, checked)
return s[0] #shortest palindrome in s
checked.add(s)
lp2 = self.longestPalindrome(s[:-1], checked)
lp1 = self.longestPalindrome(s[1:], checked)
print()
if len(lp1) > len(lp2):
return lp1
return lp2
def isPalindrome(self, s:str) -> str:
i=0
j=len(s)-1
while i<j:
if s[i]!=s[j]:
return False
i=i+1
j=j-1
return True
</code>
<code>class Solution:
def longestPalindrome(self, s: str, checked = set()) -> str:
print(s, self.isPalindrome(s))
if len(s) == 1 or self.isPalindrome(s):
return s
if s in checked:
print(s, checked)
return s[0] #shortest palindrome in s
checked.add(s)
lp2 = self.longestPalindrome(s[:-1], checked)
lp1 = self.longestPalindrome(s[1:], checked)
print()
if len(lp1) > len(lp2):
return lp1
return lp2
def isPalindrome(self, s:str) -> str:
i=0
j=len(s)-1
while i<j:
if s[i]!=s[j]:
return False
i=i+1
j=j-1
return True
</code>
class Solution:
def longestPalindrome(self, s: str, checked = set()) -> str:
print(s, self.isPalindrome(s))
if len(s) == 1 or self.isPalindrome(s):
return s
if s in checked:
print(s, checked)
return s[0] #shortest palindrome in s
checked.add(s)
lp2 = self.longestPalindrome(s[:-1], checked)
lp1 = self.longestPalindrome(s[1:], checked)
print()
if len(lp1) > len(lp2):
return lp1
return lp2
def isPalindrome(self, s:str) -> str:
i=0
j=len(s)-1
while i<j:
if s[i]!=s[j]:
return False
i=i+1
j=j-1
return True