原题链接在这里:https://leetcode.com/problems/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.

Example 1:

Input: "aabb"
Output: ["abba", "baab"]

Example 2:

Input: "abc"
Output: []

题解:

先判断是否palindrome, 若不是返回空的res.

若是,先判断是否有一个奇数位,若有,改char放中间. 然后两边分别加.

Note: check to find single char after updating the map.

Don't forget to minus its count by 1 after appending it to item.

Time Complexity: O(2^n). n = s.length().

Space: O(n). stack space.

AC Java:

 class Solution {
public List<String> generatePalindromes(String s) {
List<String> res = new ArrayList<>();
if(s == null || s.length() == 0){
return res;
} int [] count = new int[256];
int n = s.length();
int po = -1;
for(int i = 0; i<n; i++){
count[s.charAt(i)]++;
} int oneCount = 0;
for(int i = 0; i<256; i++){
oneCount += count[i]%2; if(count[i]%2 == 1){
po = i;
}
} if(oneCount > 1){
return res;
} String init = "";
if(po != -1){
init += (char)po;
count[po]--;
} dfs(count, init, n, res);
return res;
} private void dfs(int [] count, String cur, int n, List<String> res){
if(cur.length() == n){
res.add(cur);
return;
} for(int i = 0; i<count.length; i++){
if(count[i] > 0){
count[i] -= 2;
dfs(count, (char)i+cur+(char)i, n, res);
count[i] += 2;
}
}
}
}

Palindrome Permutation相似.

LeetCode Palindrome Permutation 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 I & II

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

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

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

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

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

  5. LeetCode Palindrome Permutation

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

  6. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  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#267] Palindrome Permutation II

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

  9. 267. Palindrome Permutation II

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

随机推荐

  1. http 上传文件

    @RequestMapping(method=RequestMethod.POST,value = "/upload") public ModelAndView processIm ...

  2. CNN训练Cifar-10技巧

    关于数据集 Cifar-10是由Hinton的两个大弟子Alex Krizhevsky.Ilya Sutskever收集的一个用于普适物体识别的数据集.Cifar是加拿大政府牵头投资的一个先进科学项目 ...

  3. struts1 核心类

    http://ajh-123.blog.163.com/blog/static/161727409201031455634475/ 1.ActionServlet类: 是一个前端控制器,将reques ...

  4. To Do List

    妈呀...发现不发博文公布自己要学的东西压力少太多了.......... 然后就会变得颓废..................... 求大家监督QAQ....To Do List是近3天左右目标,3天 ...

  5. 【BZOJ1968】【AHoi2005】COMMON约数研究

    Description Input 只有一行一个整数 N(0 < N < 1000000). Output 只有一行输出,为整数M,即f(1)到f(N)的累加和. Sample Input ...

  6. 关于加了hibernate 框架的项目启动特别慢的问题

    今天突然遇到一个问题,就是加了hibernate 框架的项目在启动的时候,特别慢,竟然达到了4分多钟,查来查去,看到我的bean类里*.hbm.xml,有这样的写法: <?xml version ...

  7. 测试简单for循环的效率

    os : CentOS 5.2 代码:test-usecond.c #include <stdio.h> #include <sys/time.h> // for gettim ...

  8. ArcGIS初步 系列视频教程

    本人才疏学浅,但鉴于较少的ArcGIS10以上版本的学习视频,所以利用业余时间做了这么个业余的视频系列教程,本随笔提供在线观看地址与720P原版下载地址. 1认识ArcGIS 优酷在线地址  (优酷清 ...

  9. Address already in use:JVM_Bind

    1.原因:端口被占用 2.解决方式: 方式一:重启电脑 方式二:方式一不行执行方式二 双击Tomcat server   将Ports下HTTP/1.1对应的Port Number 改为其他值 备注: ...

  10. 34款Firefox渗透测试插件工具

    工欲善必先利其器,firefox一直是各位渗透师必备的利器,小编这里推荐34款firefox渗透测试辅助插件,其中包含渗透测试.信息收集.代理.加密解密等功能. 1:Firebug Firefox的 ...