This python program runs gives correct output for aaaabaaa, but the leetcode says its giving aaaa, why?

  • 0

    Given input string aaaabaaa, I don't understand why the leetcode gives aaaa as output while on my machine it correctly outputs aaaabaaa.

     class Solution:
        def longestPalindrome(self, s):
            n = len(s)
            if n in [0, 1]:
                return s
            s2 = s[::-1]
            for i in range(1, n+1):
                for j in range(n, i, -1):
                    if i==1:
                        if s[:j] == s2[-j:]:
                            return s[:j]
                        if s[i-1:j] == s2[-j:-i+1]:
                            return s[i-1:j]

Log in to reply

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