当前位置:网站首页>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;
}
};
边栏推荐
- How to automatically push my new articles to my fans (very simple, can't learn to hit me)
- tiup telemetry
- NMS原理及其代码实现
- 性能测试如何准备测试数据
- Mysql_12 多表查询
- gorm的Raw与scan
- How to burn the KT148A voice chip into the chip through the serial port and the tools on the computer
- Mysql_14 存储引擎
- Senior game modelers tell newbies, what are the necessary software for game scene modelers?
- Metasploit-域名上线隐藏IP
猜你喜欢

Privacy Computing Overview

Mysql_13 事务

Senior game modelers tell newbies, what are the necessary software for game scene modelers?

在线中文姓名生成工具推荐
![[LeetCode] Summary of Matrix Simulation Related Topics](/img/80/bd71ca5211cce5805909015a642893.jpg)
[LeetCode] Summary of Matrix Simulation Related Topics
![[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots](/img/fa/5bdc81b1ebfc22d31f42da34427f3e.png)
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots

SV 类的虚方法 多态

2 用D435i运行VINS-fusion

数据类型及输入输出初探(C语言)

翁恺C语言程序设计网课笔记合集
随机推荐
【云原生--Kubernetes】调度约束
MAUI Blazor 权限经验分享 (定位,使用相机)
数据类型及输入输出初探(C语言)
About I double-checked and reviewed the About staff page, returning an industry question
软件测试面试题:什么是软件测试?软件测试的目的与原则?
关于我仔细检查审核过关于工作人员页面,返回一个所属行业问题
First, the basic concept of reptiles
电子行业MES管理系统的主要功能与用途
Mysql based
[idea] idea configures sql formatting
《MySQL入门很轻松》第2章:MySQL管理工具介绍
MVCC是什么
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
【Unity编译器扩展之进度条】
关于使用read table 语句
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
Senior game modelers tell newbies, what are the necessary software for game scene modelers?
Essential knowledge for entry-level 3D game modelers
E - Many Operations (按位考虑 + dp思想记录操作后的结果
倒计时1天!8月2日—4日与你聊聊开源与就业那些事!