当前位置:网站首页>leetcode: 267. Palindromic permutations II
leetcode: 267. Palindromic permutations II
2022-08-05 00:20:00 【OceanStar study notes】
题目来源
题目描述
给定一个字符串 s ,返回其通过重新排列组合后所有可能的回文字符串,并去除重复的组合.
如不能形成任何回文排列时,则返回一个空列表.
示例 1:
输入: “aabb”
输出: [“abba”, “baab”]
示例 2:
输入: “abc”
输出: []
class Solution {
public:
vector<string> generatePalindromes(string s){
}
};
题目解析
思路
- 先判断Can you form a palindrome string,如果不能,直接返回空
- 如果可以,Then backtrack to generate
The question is how to ensure that the generated string must be a palindrome?
- Due to the nature of palindrome strings,So you only need to generate the first half of the fields,The latter is directly obtained from the first half
- Also because the palindrome may be odd/偶数长度:
- Even palindrome egabba,It can be divided into two halves equally
- Odd palindrome egabcba,It needs to be divided into three parts: front, middle and back
- And the palindrome of a string needs to exist,Then an odd number of characters can only be0个或者1个,The rest must be an even number,所以:
- Use a hash table to record the number of occurrences of all characters
- Then find out the odd number of characters to joinmid中.If there are two or more odd numbers of characters,那么返回空
- 对于每个字符,Regardless of its parity,Divide it by the number2The number of characters addedt中,这样做的原因是:
- 如果是偶数个,Add half of itt中
- 如果是奇数,如果有1个,除以2是0,No characters will be addedt,如果是3个,除以2是1,take one to joint
实现
class Solution {
void process(std::string &t, int start, const std::string &min, unordered_set<string> & ans){
if(start >= t.size()){
ans.insert(t + min + std::string(t.rbegin(), t.rend()));
}
for (int i = start; i < t.size(); ++i) {
if(i != start && t[i] == t[start]){
continue;
}
std::swap(t[i], t[start]);
process(t, start + 1, min, ans);
std::swap(t[i], t[start]);
}
}
public:
vector<string> generatePalindromes(string s){
unordered_set<string> ans;
unordered_map<char, int > m;
for(auto ch : s){
m[ch]++;
}
std::string mid, t;
for(auto it : m){
if(it.second %2 == 1){
mid += it.first;
if(mid.size() > 1){
return {
};
}
}
t += std::string(it.second / 2, it.first);
}
process(t, 0, mid, ans);
return std::vector<std::string> (ans.begin(), ans.end());
}
};
实现二
class Solution {
public:
vector<string> generatePalindromes(string s) {
vector<string> res;
unordered_map<char, int> m;
string t = "", mid = "";
for (auto a : s) ++m[a];
for (auto &it : m) {
if (it.second % 2 == 1) mid += it.first;
it.second /= 2;
t += string(it.second, it.first);
if (mid.size() > 1) return {
};
}
permute(t, m, mid, "", res);
return res;
}
void permute(string &t, unordered_map<char, int> &m, string mid, string out, vector<string> &res) {
if (out.size() >= t.size()) {
res.push_back(out + mid + string(out.rbegin(), out.rend()));
return;
}
for (auto &it : m) {
if (it.second > 0) {
--it.second;
permute(t, m, mid, out + it.first, res);
++it.second;
}
}
}
};
next_permutation
class Solution {
public:
vector<string> generatePalindromes(string s) {
vector<string> res;
unordered_map<char, int> m;
string t = "", mid = "";
for (auto a : s) ++m[a];
for (auto it : m) {
if (it.second % 2 == 1) mid += it.first;
t += string(it.second / 2, it.first);
if (mid.size() > 1) return {
};
}
sort(t.begin(), t.end());
do {
res.push_back(t + mid + string(t.rbegin(), t.rend()));
} while (next_permutation(t.begin(), t.end()));
return res;
}
};
边栏推荐
猜你喜欢

刘润直播预告 | 顶级高手,如何创造财富

What is next-generation modeling (with learning materials)

Mysql_13 事务

10 个关于 Promise 和 setTimeout 知识的面试题,通过图解一次说透彻

QSunSync Qiniu cloud file synchronization tool, batch upload

电子行业MES管理系统的主要功能与用途

Implementation principle of golang coroutine

【LeetCode】矩阵模拟相关题目汇总
Handwritten Distributed Configuration Center (1)

MAUI Blazor 权限经验分享 (定位,使用相机)
随机推荐
关于我仔细检查审核过关于工作人员页面,返回一个所属行业问题
leetcode:269. 火星词典
[LeetCode] Summary of Matrix Simulation Related Topics
仿网易云音乐小程序-uniapp
uinty lua 关于异步函数的终极思想
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
Chinese and Japanese color style
Detailed explanation of common DNS resource record types
leetcode经典例题——单词拆分
【Valentine's Day special effects】--Canvas realizes full screen love
软件测试面试题:您以往所从事的软件测试工作中,是否使用了一些工具来进行软件缺陷(Bug)的管理?如果有,请结合该工具描述软件缺陷(Bug)跟踪管理的流程?
导入JankStats检测卡帧库遇到问题记录
克服项目管理中恐惧心理
After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
刘润直播预告 | 顶级高手,如何创造财富
Will domestic websites use Hong Kong servers be blocked?
NMS原理及其代码实现
MongoDB permission verification is turned on and mongoose database configuration
What is next-generation modeling (with learning materials)
阅读笔记:如何理解DevOps?