Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
palindrome-permutation-ii.cpp 2.70 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2015-08-23 16:24 +08:00 . Update palindrome-permutation-ii.cpp
// Time: O(n * n!)
// Space: O(n)
class Solution {
public:
vector<string> generatePalindromes(string s) {
if (s.empty()) {
return {};
}
unordered_map<char, int> cnt;
for (const auto& c : s) {
++cnt[c];
}
string mid, chars;
for (const auto& kvp : cnt) {
if (kvp.second % 2) {
if (mid.empty()) {
mid.push_back(kvp.first);
} else { // The count of the middle char is at most one.
return {};
}
}
chars.append(kvp.second / 2, kvp.first);
}
return permuteUnique(mid, chars);
}
vector<string> permuteUnique(const string& mid, string& chars) {
vector<string> result;
sort(chars.begin(), chars.end());
do {
string reverse_chars(chars.crbegin(), chars.crend());
result.emplace_back(chars + mid + reverse_chars);
} while (next_permutation(chars.begin(), chars.end()));
return result;
}
};
class Solution2 {
public:
vector<string> generatePalindromes(string s) {
if (s.empty()) {
return {};
}
unordered_map<char, int> cnt;
for (const auto& c : s) {
++cnt[c];
}
string mid, chars;
for (const auto& kvp : cnt) {
if (kvp.second % 2) {
if (mid.empty()) {
mid.append(1, kvp.first);
} else { // The count of the middle char is at most one.
return {};
}
}
chars.append(kvp.second / 2, kvp.first);
}
return permuteUnique(mid, chars);
}
vector<string> permuteUnique(const string& mid, string& s) {
vector<string> result;
vector<bool> used(s.length(), false);
string ans;
sort(s.begin(), s.end());
permuteUniqueRecu(mid, s, &used, &ans, &result);
return result;
}
void permuteUniqueRecu(const string& mid, const string& s, vector<bool> *used,
string *ans, vector<string> *result) {
if (ans->length() == s.length()) {
string reverse_ans(ans->crbegin(), ans->crend());
result->emplace_back(*ans + mid + reverse_ans);
return;
}
for (int i = 0; i < s.length(); ++i) {
if (!(*used)[i] && !(i != 0 && s[i - 1] == s[i] && (*used)[i - 1])) {
(*used)[i] = true;
ans->push_back(s[i]);
permuteUniqueRecu(mid, s, used, ans, result);
ans->pop_back();
(*used)[i] = false;
}
}
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助