[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: ...
随机推荐
- 设置ORACLE环境变量
sqlplus 执行不了可能原因是未设置环境变量,设置方法: export ORACLE_HOME=/usr/local/instantclient_11_2
- SQL Server死锁日志各字段含义
使用跟踪标记 1204 --打开跟踪标记 DBCC TRACEON (1204,-1) --关闭跟踪标记 DBCC TRACEOFF (1204,-1) 处于死锁状态时,跟踪标记 1204 在等待的线 ...
- 从ActionFilterAttribute向view传送数据
[原文转载]http://www.cnblogs.com/QLeelulu/archive/2008/08/17/1269599.html 原文地址:ASP.NET MVC Tip #31 – Pas ...
- oracle decode函数使用方法
1.decode(V1,1,A,2,B,C) 如果V1=1 那么显示A =2显示B 其他显示C ........ 2. 含义解释: decode(条件,值1,返回值1,值2,返回值2,...值n,返 ...
- iOS 代码分类
控件分类: 指示器 (ActivityIndicator) 提醒对话框 (AlertView) 按钮 (Button) 日历 (Calendar) 相机 (Camera) 透明指示层 (HUD) 图像 ...
- objectiv-c所有对象之间的交互是如何实现的?
在对象间交互中每个对象承担不同的角色,总的来说就是“数据发送者”和“数据接收者”两个角色.可以通过objective-c中给我们提供的手段来实现两者间的通讯.比如: “通知中心”NSNotificat ...
- 如何获取App当前版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *currentVersion = [infoDic ...
- nginx——location 优先级
一. location 的匹配符1.等于匹配符:=等于匹配符就是等号,特点可以概括为两点:精确匹配不支持正则表达式2.空匹配符空匹配符的特点是:匹配以指定模式开始的 URI不支持正则表达式3.正则匹配 ...
- CSS远程加载字体
CSS 远程加载字体的方法,做网站CSS的都知道,用户浏览网站时,网页上的字体是加载本地的.换言之,如果网站使用了用户电脑所没有安装的字体,那显示字体就会被默认字体所代替了,自然效果就大受影响了. 上 ...
- TatukGIS - GisDefs - CheckDir 函数
函数名称 CheckDir 所在单元 GisDefs 函数原型 function CheckDir(const _path: String): Boolean; 函数说明 如果 _path ...