Python Easy Solution


  • 0
    D
    from collections import Counter
    class Solution(object):
        def generatePalindromes(self, s):
            """
            :type s: str
            :rtype: List[str]
            """
            c = Counter(s)
            values = c.values()
            odds = [(k, c[k]) for k in c if c[k]%2 == 1]
            if len(odds) > 1: return []
            
            ans = []
            def collect(curr, counts):
                remain_chars = [k for k in counts if counts[k] > 0]
                if remain_chars == []: 
                    ans.append(curr)
                    return
                for ch in remain_chars:
                    counts[ch] -= 2
                    collect(ch + curr + ch, counts)
                    counts[ch] += 2
            if odds:
                ch = odds[0][0]
                c[ch] -= 1
            else: ch = ''
            collect(ch, c)
            return ans

Log in to reply
 

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