[LeetCode] Palindrome Permutation I & II
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
class Solution {
public:
bool canPermutePalindrome(string s) {
vector<int> cnt(, );
for (auto a : s) ++cnt[a];
bool flag = false;
for (auto n : cnt) if (n & ) {
if (!flag) flag = true;
else return false;
}
return true;
}
};
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.
没按提示来,直接用的DFS,不知道符不符合要求。
class Solution {
public:
void dfs(vector<string> &res, vector<int> &cnt, string &s, int l, int r) {
if (l >= r) {
res.push_back(s);
return;
}
for (int i = ; i < cnt.size(); ++i) if (cnt[i] >= ) {
cnt[i] -= ;
s[l] = s[r] = i;
dfs(res, cnt, s, l + , r - );
cnt[i] += ;
}
}
vector<string> generatePalindromes(string s) {
vector<int> cnt(, );
for (auto a : s) ++cnt[a];
bool flag = false;
for (int i = ; i < cnt.size(); ++i) if (cnt[i] & ) {
if (!flag) {
flag = true;
s[s.length() / ] = i;
} else {
return {};
}
}
vector<string> res;
dfs(res, cnt, s, , s.length() - );
return res;
}
};
[LeetCode] Palindrome Permutation I & II的更多相关文章
- [LeetCode] Palindrome Permutation 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] 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 ...
- [Locked] Palindrome Permutation I & II
Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...
- Leetcode: Palindrome Partition I II
题目一, 题目二 思路 1. 第一遍做时就参考别人的, 现在又忘记了 做的时候使用的是二维动态规划, 超时加超内存 2. 只当 string 左部分是回文的时候才有可能减少 cut 3. 一维动规. ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 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
Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
随机推荐
- 【Oracle】RAC下的一些经常使用命令(一)
节点层: olsnodes -n:显示每一个节点编号. [oracle@rac1 ~]# olsnodes -n rac1 1 rac2 2 -p:显示每一个节点用于private int ...
- 【Linux】cat充当vi使用(特殊用法)
1.在终端输入cat >b.txt Linux:/qinys/data # cat >b.txt 2.回车后按下ctrl+d组合键即可完成编辑
- WD backup西部盘数据备份
西部数据(WD),硬盘备份数据!防止数据丢失.损坏.... 起因: 电脑上存储很多资料,之前有500G的东芝硬盘(现在插头不灵敏),故决定换个好点的1T硬盘.电脑在一夜间打不开,不能识别硬盘!怎么重启 ...
- 【Mysql】php执行脚本进行mysql数据库 备份和还原
一.mysql备份 1.这里使用 php脚本的形式进行mysql 数据库的备份和还原,想看linux的sh版本的,有时间再贴. 2.找到 mysql的[mysqldump] 执行程序,建议phpinf ...
- java中转义字符和路径符
来源于:http://blog.csdn.net/u011479200/article/details/69062343 在Java的实际开发中,经常会遇填写一个文件的相对路径或者是绝对路径的问题,对 ...
- Easyui入门视频教程 第09集---登录完善 图标自定义
目录 ----------------------- Easyui入门视频教程 第09集---登录完善 图标自定义 Easyui入门视频教程 第08集---登录实现 ajax button的使用 ...
- ios中打包
第一步:这里需要注意,要选择真机,否则Archive 会是灰色的. 点击后,系统会自动编译一次,并跳转到如图界面: 第二步: 在你刚刚生成的程序上点击右键,并且点击Show in Finder. ...
- std::thread 不 join
std::thread 构造之后 使用 detach.就可以了
- kickstart命令选项
下面的选项可以放入kickstart文件.如果喜欢使用图形化的界面来创建kickstart文件,可以使用"Kickstart配置"应用程序.(注:如果某选项后面跟随了一个等号(=) ...
- 如何使用SetTimer
1.SetTimer定义在那里? SetTimer表示的是定义个定时器.根据定义指定的窗口,在指定的窗口(CWnd)中实现OnTimer事件,这样,就可以相应事件了. SetTimer有两个函数.一个 ...