-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path267 Palindrome Permutation II.py
51 lines (41 loc) · 1.21 KB
/
267 Palindrome Permutation II.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Premium Question
https://leetcode.com/problems/palindrome-permutation-ii/
Author: Rajeev Ranjan
"""
from collections import defaultdict
class Solution(object):
def generatePalindromes(self, s):
"""
:type s: str
:rtype: List[str]
"""
m = defaultdict(int)
for c in s:
m[c] += 1
odd = None
for k, v in m.items():
if v % 2 == 1:
if odd is not None:
return []
odd = k
cur = ""
if odd:
m[odd] -= 1
cur = odd
ret = []
# actually only need to build half
self.grow(s, m, None, cur, ret)
return ret
def grow(self, s, count_map, pi, cur, ret):
if len(cur) == len(s):
ret.append(cur)
return
for k in count_map.keys():
if k != pi and count_map[k] > 0:
for i in xrange(1, count_map[k]/2+1): # jump the parent
count_map[k] -= i*2
self.grow(s, count_map, k, k*i+cur+k*i, ret)
count_map[k] += i*2
if __name__ == "__main__":
assert Solution().generatePalindromes("aabb") == ['baab', 'abba']