Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome.

For example,
"code" -> False, "aab" -> True, "carerac" -> True.

Hint:

    1. Consider the palindromes of odd vs even length. What difference do you notice?
    2. Count the frequency of each character.
    3. 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;
}
};

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:

  1. If a palindromic permutation exists, we just need to generate the first half of the string.
  2. 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的更多相关文章

  1. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  2. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. LeetCode Palindrome Permutation

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...

  5. [Locked] Palindrome Permutation I & II

    Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...

  6. Leetcode: Palindrome Partition I II

    题目一, 题目二 思路 1. 第一遍做时就参考别人的, 现在又忘记了 做的时候使用的是二维动态规划, 超时加超内存 2. 只当 string 左部分是回文的时候才有可能减少 cut 3. 一维动规. ...

  7. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  8. leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II

    266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...

  9. [LeetCode#267] Palindrome Permutation II

    Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

随机推荐

  1. Maven依赖传递、依赖传递排除、依赖冲突

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6628429.html  一:Maven依赖传递 假如有Maven项目A,项目B依赖A,项目C依赖B.那么我们可 ...

  2. java 生成二维码后叠加LOGO并转换成base64

      1.代码 见文末推荐 2.测试 测试1:生成base64码 public static void main(String[] args) throws Exception { String dat ...

  3. 6、redis之使用spring-data-redis的Template

    POM: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  4. IBM应该请我去做Domino产品设计架构师

                                  作者: 亓锋                         2012年11月19日16:56  来源: 速途专栏              ...

  5. LevelDB和ForestDB简单性能测试(含代码)

    测试环境简单说明 Windows下测试 硬件环境如下: 处理器:Intel(R) Core(TM) i5-4460 CPU @ 3.20GHz 内 存:8GB 硬 盘:希捷 ST1000DM003 操 ...

  6. java第五节 多线程/多线程的同步

    /* 第五讲 多线程 了解进程和线程 在多任务系统中,每个独立执行的程序称为进程,也就是"正在进行的程序",我们现在使用的操作系统一般都是多任务的 即能够同时执行多个应用程序,实际 ...

  7. Tomcat服务器版本号泄露-低危漏洞修复

    一.问题描述Tomcat报错页面泄漏Apache Tomcat/7.0.52相关版本号信息,是攻击者攻击的途径之一.因此实际当中建议去掉版本号信息.二.解决办法 1.进入到tomcat/lib目录下, ...

  8. C语言中的随意跳转

    C语言中有一个很不常用的头文件:setjmp.h. 这个头文件是C语言底层实现的,不像math.h里面的函数都是纯C语言实现的. setjmp.h包含两个函数: longjmp 跳转到某个位置 set ...

  9. 10行代码解析krc歌词文件

    互联网上,我们常见的歌词格式有 LRC.TRC(天天动听歌词).KRC(KuGou ResourCe,酷狗资源文件)和 QRC(QQ音乐歌词):在影视制作中,人们通常会用其他的卡拉 OK 字幕格式,例 ...

  10. Linux下Valgrind的使用概述 来源:Linux社区 作者:dndxhej

    本地使用情况,在Ubantu系统上编写的测试性能: 1,下载:sudo apt-get install valgrind; 2,验证是否安装成功:试着valgrind ls -l来检测是否正常工作: ...