当前位置:网站首页>leetcode:267. 回文排列 II
leetcode:267. 回文排列 II
2022-08-05 00:10:00 【OceanStar的学习笔记】
题目来源
题目描述
给定一个字符串 s ,返回其通过重新排列组合后所有可能的回文字符串,并去除重复的组合。
如不能形成任何回文排列时,则返回一个空列表。
示例 1:
输入: “aabb”
输出: [“abba”, “baab”]
示例 2:
输入: “abc”
输出: []
class Solution {
public:
vector<string> generatePalindromes(string s){
}
};
题目解析
思路
- 先判断能不能组成回文字符串,如果不能,直接返回空
- 如果可以,再回溯生成
问题是怎么确保生成的一定是回文串呢?
- 由于回文串的特性,所以只需要生成前半段字段即可,后面的直接根据前半段得到
- 又因为回文串可能是奇数/偶数长度:
- 偶数回文串比如abba,可以平均分成前后半段
- 奇数回文串比如abcba,需要分成前中后三段
- 而一个字符串的回文串要存在,那么奇数个的字符只能是0个或者1个,其余的必须是偶数个,所以:
- 用哈希表来记录所有的字符出现的个数
- 然后找出出现奇数次的字符加入mid中。如果有两个或者两个以上的奇数个数的字符,那么返回空
- 对于每个字符,不管其奇偶,都将其个数除以2的个数的字符加入t中,这样做的原因是:
- 如果是偶数个,将其一半加入t中
- 如果是奇数,如果有1个,除以2是0,不会有字符加入t,如果是3个,除以2是1,取一个加入t
实现
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;
}
};
边栏推荐
- 安全软件 Avast 与赛门铁克诺顿 NortonLifeLock 合并案获英国批准,市值暴涨 43%
- 学会反射后,我被录取了(干货)
- KT148A voice chip ic working principle and internal architecture description of the chip
- Cloud native - Kubernetes 】 【 scheduling constraints
- Handwritten Distributed Configuration Center (1)
- Bidding Announcement | Operation and Maintenance Project of Haina Baichuang Official Account
- 标识符、关键字、常量 和变量(C语言)
- Basic web in PLSQL
- 【云原生--Kubernetes】Pod控制器
- Cython
猜你喜欢

uniapp sharing function - share to friends group chat circle of friends effect (sorting)

学会反射后,我被录取了(干货)
情人节---快来学习一下程序员的专属浪漫吧

Mysql_13 事务

leetcode经典例题——单词拆分

Day118. Shangyitong: order list, details, payment

Essential knowledge for entry-level 3D game modelers

MongoDB permission verification is turned on and mongoose database configuration

美团二面:Redis与MySQL双写一致性如何保证?

在线中文姓名生成工具推荐
随机推荐
数据类型-整型(C语言)
10 种常见的BUG分类
Huggingface入门篇 II (QA)
IDEA 文件编码修改
【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
Chinese and Japanese color style
手写分布式配置中心(1)
安全软件 Avast 与赛门铁克诺顿 NortonLifeLock 合并案获英国批准,市值暴涨 43%
没有这些「伪需求」,产品经理的 KPI 怎么完成?
Ab3d.PowerToys and Ab3d.DXEngine Crack
Couple Holding Hands [Greedy & Abstract]
小黑leetcode冲浪:94. 二叉树的中序遍历
RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
《MySQL入门很轻松》第2章:MySQL管理工具介绍
情人节---快来学习一下程序员的专属浪漫吧
阅读笔记:如何理解DevOps?
【CVA估值训练营】财务建模指南——第一讲
The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
KT148A语音芯片ic工作原理以及芯片的内部架构描述
#yyds干货盘点#交换设备丢包严重的故障处理