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:

  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

分析:

  这个问题不需要判断是否是回文字符串,而是判断是否能组成回文字符串,换句话说就是字母在原字符串中的顺序无关。

解法:

  可根据回文定义得出,即允许出现奇数次的字母种数最多为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:

  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 fromPermutations 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的更多相关文章

  1. [LeetCode] Palindrome Permutation I & II

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

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

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

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

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

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

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

  5. LeetCode Palindrome Permutation II

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

  6. [LeetCode#267] Palindrome Permutation II

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

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

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

  8. LeetCode Palindrome Permutation

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

  9. [LeetCode] 266. Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...

随机推荐

  1. HTML5+移动APP(1)

    前言: 介绍使用html5+(nativejs)和mui开发移动app(包括Android和iOs) HBuilder h5+开发app的环境,是一个对eclipse做了深度定的IDE. 官网: ht ...

  2. 惠普 Compaq 6520s 无线开关打不开

    问题描述:键盘上面的无线开关怎么按都打不开,始终是出于黄色的状态   解决方法:尝试恢复bios默认值测试: 开机不停点击F10进入bios,选择File选项,选择Restore Defaults-- ...

  3. 用户组,AD域控简介

    “自由”的工作组    工作组(WORK GROUP)就是将不同的电脑按功能分别列入不同的组中,以方便管理.比如在一个网络内,可能有成百上千台工作电脑,如果这些电脑不进行分组,都列在“网上邻居”内,可 ...

  4. shell脚本学习之case例子

    case和select结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支.  在代码块中控制程序分支  ca ...

  5. block 浅析

    最近讲了一个关于block的例子 block 可以作为一个参数 进行传递 需要注意的地方是 :block 虽然作为一个参数 但是在函数方法执行的时候 block 是不会在定义它的地方调用 除非你在后边 ...

  6. [转]mysql 导入导出数据库以及函数、存储过程的介绍

    本篇文章是对mysql中的导入导出数据库命令以及函数.存储过程进行了详细的分析介绍,需要的朋友参考下: mysql常用导出数据命令:1.mysql导出整个数据库  mysqldump -hhostna ...

  7. IBM服务器 IMM日志收集

    在IBM X系列服务器背板上有一个" SYS MGMT"接口,用网线将此接口与笔记本连接起来,然后将笔记本的 IP地址配置成 192.168.70.0/24 这个网段的地址,然后 ...

  8. SGU 122.The book (哈密顿回路)

    题目描述 有一群人从1到N标号,而且这群人中每个人的朋友个数不少于 (N+1)/2 个. 编号为1的人有一本其他人都想阅读的书. 写一个程序,找到一种传阅顺序使得书本只经过每个人手中一次,并且一个人只 ...

  9. jQuery慢慢啃之特效(八)

    1.show([speed,[easing],[fn]])\\显示隐藏的匹配元素 //speed:三种预定速度之一的字符串("slow","normal", o ...

  10. Mac OS X 系统下快速显示隐藏文件的方法(使用Automator创建workflow)

    有的时候需要显系统中的隐藏文件,在 Mac 中不像windows系统那么方便(勾选选项就能够操作),需要在 Terminal 中执行: localhost:~ mx$ defaults write c ...