[Locked] Palindrome Permutation I & II
Palindrome Permutation I
Given a string, determine if a permutation of the string could form a palindrome.
For example,"code" -> False, "aab" -> True, "carerac" -> True.
Hint:
- Consider the palindromes of odd vs even length. What difference do you notice?
- Count the frequency of each character.
- If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times
分析:
这个问题不需要判断是否是回文字符串,而是判断是否能组成回文字符串,换句话说就是字母在原字符串中的顺序无关。
解法:
可根据回文定义得出,即允许出现奇数次的字母种数最多为1
证明:
充分性,将出现奇数次的字母放在中间,若无出现奇数次的字母,则直接做下一步,然后从中间向两边依次放置出现偶数次的字母,满足;
必要性,任意回文字符串都满足中轴对称,偶数个字母则有出现奇数次的字母种数为0,奇数个字母则有出现奇数次的字母种数为1,满足;
代码:
bool isPermutation(string str){
vector<char> bin( ,);
for(char c : str)
bin[int(c - 'a')] ^= ;
int count = ;
for(int i : bin)
count += i;
return count <= ;
}
Palindrome Permutation II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.
For example:
Given s = "aabb", return ["abba", "baab"].
Given s = "abc", return [].
Hint:
- If a palindromic permutation exists, we just need to generate the first half of the string.
- To generate all distinct permutations of a (half of) string, use a similar approach from: Permutations II or Next Permutation.
分析:
这个问题相比上个问题,是个后续输出工作,直接排列所有情况即可,证明比较直观。
解法:
直接排列。小技巧同Hint. 1给出的,只需要得出一边的排列。
代码:
void dfs(unordered_set<string> &uset, string str, vector<int> bin, int total) {
if(total == ) {
uset.insert(str);
return;
}
for(int i = ; i < bin.size(); i++) {
if(bin[i] == )
continue;
bin[i]--;
dfs(uset, str + char(i + 'a'), bin, total - );
bin[i]++;
}
return;
}
vector<string> permutation(string str){
vector<int> bin( ,);
for(char c : str)
bin[int(c - 'a')]++;
int count = , total = ;
char record;
for(int i = ; i < bin.size(); i++) {
total += bin[i];
if((bin[i] & ) == ) {
record = char(i + 'a');
count++;
}
}
vector<string> vs;
if(count > )
return vs;
for(int &i : bin)
i /= ;
unordered_set<string> uset;
dfs(uset, "", bin, total / );
for(string s : uset) {
string str = s;
if(count == )
str += record;
reverse(s.begin(), s.end());
str += s;
vs.push_back(str);
}
return vs;
}
[Locked] Palindrome Permutation I & II的更多相关文章
- [LeetCode] Palindrome Permutation I & II
Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- [LeetCode#267] Palindrome Permutation II
Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- LeetCode Palindrome Permutation
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
随机推荐
- 在企业级开发中使用Try...Catch...会影响效率吗?
感谢神啊.上帝及老天爷让我失眠,才能够有了本篇文章. 记得不久之前,公司一同事曾经说过:“如果是Winform开发,由于程序是在本地,使用try...catch不会有太大性能问题,可是如果是在web服 ...
- switch语法之PHP
$a = 100; switch ($a) { case 100: echo '满分'; break; case $a >=60: echo '及格'; break; }
- 用于显示上个月和下个月_PHP
/** * 用于显示上个月和下个月 * @param int $sign 1:表示上个月 0:表示下个月 * @return string */ function GetMonth($sign=&qu ...
- 修改docker默认172.17网段
docker启动时默认使用172.17.x.x作为容器的ip地址,可以通过以下方法自定义该网段: sudo service docker stop通过命令route -n查看docker0是否存在,若 ...
- 使用AutoMapper实现Dto和Model之间自由转换
应用场景:一个Web应用通过前端收集用户的输入成为Dto,然后将Dto转换成领域模型并持久化到数据库中.另一方面,当用户请求数据时,我们又需要做相反的工作:将从数据库中查询出来的领域模型以相反的方式转 ...
- php 文件上传后缀名与文件类型对照表(几乎涵盖所有文件)
网上有很多php文件上传的类,文件上传处理是php的一个特色(至少手册上是将此作为php特点来展示的,个人认为php在数组方面的优异功能更有特 色),学php的人都知道文件上传怎么做,但很多人在编程中 ...
- Python 基础-python函数
函数 1.def 2.命名 3.函数体 4.return 返回值 def get_return(): a = 1 return a 函数参数有 形参和实参 定义几个形参就 ...
- Android 之夜间模式(多主题)的实现
引言 夜间模式其实属于多主题切换的一种,不过是最麻烦的一种.因为在夜间模式下不仅要切换主色调,次要色调等等,还要覆盖一些特殊的颜色,因为在夜间模式下总不能什么都是黑的把,那不得丑死-.-,所以当你夜间 ...
- iOS判断手机中是否 有 SIM卡---备用
[CTSIMSupportGetSIMStatus() isEqualToString:kCTSIMSupportSIMStatusNotInserted]可以判断是否插入了sim卡. 前提是把下面的 ...
- 2、MyBatis.NET学习笔记之CodeSmith使用
说明:本系列随笔会与CSDN同步发布,当然这里先发,因为这里可以用WLW.但刚才由于误操作,没有重新发上来.只好先在CSDN先发了.重往这里发时图片无法处理,索性直接粘过来吧! 使用框架后一些相关的配 ...