Test case is not correct: "abb" should be "abba", not "bbabb"

  • -1
    class Solution(object):
        def shortestPalindrome(self, s):
            :type s: str
            :rtype: str
            ps, rest = '', s
            while rest and rest[0]==rest[-1] and len(rest)>1:
                ps += rest[0]
                rest = rest[1:-1]
            if len(rest)<=1:
                return ps + rest + ps[::-1]
            ls = ps + self.shortestPalindrome(rest+rest[0]) + ps
            rs = ps + self.shortestPalindrome(rest[-1]+rest) + ps
            return rs if len(ls)>=len(rs) else ls

Log in to reply

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