题目:
Given a string s
, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.
For example:
Given s = "aabb"
, return ["abba", "baab"]
.
Given s = "abc"
, return []
.
Hint:
按照题目里的提示一步步做即可。
C++版:
class Solution { public: vector<string> generatePalindromes(string s) { vector<string> results; if(s.length() == 0) return results; unordered_map<char, int> d; for(auto i : s) { if(d.find(i) != d.end()) d[i]++; else d.insert(pair<char, int>(i, 1)); } bool already = false; string candidate = ""; string single = ""; for(auto i = d.begin(); i != d.end(); i++) { int num = i->second / 2; for(int j = 0; j < num; j++) candidate += i->first; if(i->second % 2 != 0) { if(already) return results; else { already = true; single += i->first; } } } if(candidate.length() == 0 && single.length() != 0) { results.push_back(single); return results; } string left = ""; int l = candidate.length(); recursion(left, candidate, single, l, results); return results; } void recursion(string left, string candidate, string &single, int &l, vector<string> &results) { if(left.length() == l) { string result = left + single; reverse(left.begin(), left.end()); result += left; results.push_back(result); return; } for(int i = 0; i < candidate.length(); i++) { if(i > 0 && candidate[i] == candidate[i - 1]) continue; recursion(left + candidate[i], candidate.substr(0, i) + candidate.substr(i + 1), single, l, results); } } };
public class Solution { public List<String> generatePalindromes(String s) { List<String> results = new ArrayList<>(); if(s.length() == 0) return results; HashMap<Character, Integer> d = new HashMap<>(); for(int i = 0; i < s.length(); i++) { if(d.containsKey(s.charAt(i))) d.put(s.charAt(i), d.get(s.charAt(i)) + 1); else d.put(s.charAt(i), 1); } String candidate = ""; String single = ""; boolean already = false; for(Character c : d.keySet()) { int num = d.get(c) / 2; for(int i = 0; i < num; i++) candidate += c; if(d.get(c) % 2 != 0) { if(already) return results; else { already = true; single += c; } } } if(candidate.length() == 0 && single.length() != 0) { results.add(single); return results; } recursion("", candidate, single, candidate.length(), results); return results; } private void recursion(String left, String candidate, String single, int l, List<String> results) { if(left.length() == l) { String right = new StringBuffer(left).reverse().toString(); results.add(left + single + right); } for(int i = 0; i < candidate.length(); i++) { if(i > 0 && candidate.charAt(i) == candidate.charAt(i - 1)) continue; recursion(left + candidate.charAt(i), candidate.substring(0, i) + candidate.substring(i + 1), single, l, results); } } }
class Solution(object): def __init__(self): self.results = [] def generatePalindromes(self, s): """ :type s: str :rtype: List[str] """ d = {} for i in s: if i not in d: d[i] = 1 else: d[i] += 1 already, candidate, single = False, "", "" for i in d: num = d[i] / 2 for j in range(num): candidate += i if d[i] % 2 != 0: if already: return [] else: already = True single += i if len(candidate) == 0 and len(single) != 0: self.results.append(single) return self.results for i in range(len(candidate)): if i > 0 and candidate[i] == candidate[i - 1]: continue self.recursion(candidate[i], candidate[:i] + candidate[i+1:], len(candidate), single) return self.results def recursion(self, left, candidate, l, single): if len(left) == l: self.results.append(left + single + left[::-1]) return for i in range(len(candidate)): if i > 0 and candidate[i] == candidate[i - 1]: continue self.recursion(left + candidate[i], candidate[:i] + candidate[i+1:], l, single)