[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 ...
随机推荐
- Android学习笔记八:用Broadcast Receiver跨进程(跨app)通信
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7515194.html 在前面介绍四大组件的时候提到了可以对外部事件进行过滤的Broadcast Receive ...
- JSP页面跳转之sendRedirect()与forward()辨析
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6044817.html 在JSP中,要实现页面的跳转,主要有两种方式实现:forward和sendRedire ...
- Oracle官方文档
Oracle DBA 10g 两日速成课程 http://www.oracle.com/webfolder/technetwork/cn/tutorials/obe/db/10g/r2/2day_db ...
- java的配置方式简介
1,java的配置方式简介java的配置方式是为了代替使用xml配置方式,主要使用两个注解:@Configuration//通过该注解来表明该类是一个spring的配置,相当于一个xml文件@Comp ...
- Eclipse和MyEclipse使用技巧--Eclipse各版本介绍
进入eclipse的下载官网 http://www.eclipse.org/downloads/ 发现,会有多种版本提供下载. 对于刚接触Java开发的初学者,在下载eclipse时,对官网上面提 ...
- Window修改cmd编码
Window默认编码是gbk,对一些字符不支持.需在不同语言上切换,急需要调整字符集编码..chcp 功能: 显示或设置活动代码页编号 chcp [nnn] #nnn 指定的代码页编号 chcp ...
- 使用httpClient调用接口获取响应数据
转自:https://blog.csdn.net/shuaishuaidewo/article/details/81136088 import lombok.extern.slf4j.Slf4j; i ...
- Ubuntu菜鸟入门(十六)—— 安装视频播放器vlc
sudo add-apt-repository ppa:videolan/master-daily sudo apt-get update sudo apt-get install vlc Ubunt ...
- kubernetes删除pod失败
一.概述 k8s中删除pod失败,可能是该pod有rc,rs上层控制,而且很有可能,所以删除上层对应的rc,rs,deployment即可: 删除的方法: 1.直接删除rc,rs,deployment ...
- GitHub Desktop 代码库管理工具
1.GitHub Desktop 简介 GitHub Desktop 是用于 GitHub 项目版本控制软件. 官网下载地址 GitHub Desktop 其它下载地址 GitHub Desktop ...