LeetCode 题解(242) : Palindrome Permutation II
来源:互联网 发布:崩坏3rd初始号淘宝 编辑:程序博客网 时间:2024/05/20 14:42
题目:
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:
- If a palindromic permutation exists, we just need to generate the first half of the string.
- To generate all distinct permutations of a (half of) string, use a similar approach from:Permutations II orNext Permutation.
按照题目里的提示一步步做即可。
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); } }};
Java版:
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); } }}
Python版:
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)
0 0
- LeetCode 题解(242) : Palindrome Permutation II
- LeetCode 题解(241) : Palindrome Permutation
- [LeetCode 267] Palindrome Permutation II
- LeetCode 267. Palindrome Permutation II
- LeetCode 267. Palindrome Permutation II
- Leetcode Palindrome Permutation I & II
- Palindrome Partitioning II|leetcode题解
- LeetCode - Palindrome Partitioning II 题解
- [leetcode] 267. Palindrome Permutation II 解题报告
- [Leetcode] 267. Palindrome Permutation II 解题报告
- LeetCode 题解(90): Palindrome Partitioning II
- [LeetCode267]Palindrome Permutation II
- Palindrome Permutation II
- Palindrome Permutation II
- Palindrome Permutation II
- 267. Palindrome Permutation II
- 267. Palindrome Permutation II
- leetcode题解-125. Valid Palindrome && 680. Valid Palindrome II
- Text Justification
- SQUARE Probelm_codewars daily train9/27
- js对节点的操作,添加,删除,获得父节点,子节点,兄弟节点
- django admin 报错没有发现 table
- Handler消息传递机制
- LeetCode 题解(242) : Palindrome Permutation II
- Binary Search
- [Leetcode] Word Break
- 晶联讯1353显示屏测试程序
- Python 之包(Package)与模(Module)
- Bringing up interface eth0: Device eth0 does not seem to be present, delaying initialization.
- Linux上安装MySQL时出现不兼容的解决办法
- LeetCode 题解(243) : Paint Fence
- LeetCode 题解(244) : Missing Ranges