当前位置:网站首页>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;
}
};
边栏推荐
- 找不到DiscoveryClient类型的Bean
- The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
- gorm联表查询-实战
- node使用redis
- tiup update
- How to automatically push my new articles to my fans (very simple, can't learn to hit me)
- what is MVCC
- lua 如何 实现一个unity协程的工具
- 软件测试面试题:软件都有多少种分类?
- oracle创建用户
猜你喜欢

【LeetCode】双指针题解汇总

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

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

怎样进行在不改变主线程执行的时候,进行日志的记录
![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)
[idea] idea configures sql formatting

TinyMCE disable escape

STC89C52RC的P4口的应用问题

Three tips for you to successfully get started with 3D modeling

【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP

导入JankStats检测卡帧库遇到问题记录
随机推荐
First, the basic concept of reptiles
软件测试面试题:什么是软件测试?软件测试的目的与原则?
Cython
Cloud native - Kubernetes 】 【 scheduling constraints
E - Many Operations (按位考虑 + dp思想记录操作后的结果
进程间通信和线程间通信
oracle创建用户
数据类型-整型(C语言)
【Valentine's Day special effects】--Canvas realizes full screen love
About I double-checked and reviewed the About staff page, returning an industry question
tiup telemetry
图解 Canvas 入门
MVCC是什么
How to burn the KT148A voice chip into the chip through the serial port and the tools on the computer
软件测试面试题:设计测试用例时应该考虑哪些方面,即不同的测试用例针对那些方面进行测试?
软件测试面试题:网络七层协仪具体?
gorm联表查询-实战
Implementation principle of golang coroutine
uinty lua 关于异步函数的终极思想
After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!