1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
palindrome-permutation-ii.py 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 7年前 . add complexity
# Time: O(n * n!)
# Space: O(n)
import collections
import itertools
class Solution(object):
def generatePalindromes(self, s):
"""
:type s: str
:rtype: List[str]
"""
cnt = collections.Counter(s)
mid = ''.join(k for k, v in cnt.iteritems() if v % 2)
chars = ''.join(k * (v / 2) for k, v in cnt.iteritems())
return self.permuteUnique(mid, chars) if len(mid) < 2 else []
def permuteUnique(self, mid, nums):
result = []
used = [False] * len(nums)
self.permuteUniqueRecu(mid, result, used, [], nums)
return result
def permuteUniqueRecu(self, mid, result, used, cur, nums):
if len(cur) == len(nums):
half_palindrome = ''.join(cur)
result.append(half_palindrome + mid + half_palindrome[::-1])
return
for i in xrange(len(nums)):
if not used[i] and not (i > 0 and nums[i-1] == nums[i] and used[i-1]):
used[i] = True
cur.append(nums[i])
self.permuteUniqueRecu(mid, result, used, cur, nums)
cur.pop()
used[i] = False
class Solution2(object):
def generatePalindromes(self, s):
"""
:type s: str
:rtype: List[str]
"""
cnt = collections.Counter(s)
mid = tuple(k for k, v in cnt.iteritems() if v % 2)
chars = ''.join(k * (v / 2) for k, v in cnt.iteritems())
return [''.join(half_palindrome + mid + half_palindrome[::-1]) \
for half_palindrome in set(itertools.permutations(chars))] if len(mid) < 2 else []
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助