My Python Solution


  • 0
    class Solution(object):
        def generatePalindromes(self, s):
            letters,ans,path = collections.Counter(s),[],[]
            middle = [c for c in letters if letters[c]%2==1]
            if len(middle) > 1: return []
            if middle: letters[middle[0]] -= 1
            def gen(total):
                if total==0:
                    ans.append(''.join(path+middle+list(reversed(path))))
                for c in letters:
                    if letters[c] <= 0: continue
                    letters[c] -= 2
                    path.append(c)
                    gen(total-2)
                    letters[c] += 2
                    path.pop()
            gen(len(s)-1 if middle else len(s))
            return ans

Log in to reply
 

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