Problem:

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 [].

Analysis:

This problem is very easy, it shares the same idea with "Strobogrammatic Number".
Use the rule you check a "Palindrome Permutation" to construct it!!! (two pointer!) I have made one mistake in implementation.
Forget that when "len == 1", the string must a pilindrome string! Wrong part:
if (len <= 1) {
return ret;
} Errors:
Input:
"a"
Output:
[]
Expected:
["a"]

Solution:

public class Solution {
public List<String> generatePalindromes(String s) {
if (s == null)
throw new IllegalArgumentException("s is null");
List<String> ret = new ArrayList<String> ();
int len = s.length();
if (len == 0) {
return ret;
}
HashMap<Character, Integer> map = new HashMap<Character, Integer> ();
for (char c : s.toCharArray()) {
if (map.containsKey(c))
map.put(c, map.get(c)+1);
else
map.put(c, 1);
}
int odd_count = 0;
char odd_char = 'a';
for (char c : map.keySet()) {
if (map.get(c) % 2 == 1) {
odd_count++;
odd_char = c;
}
}
if (odd_count >= 2)
return ret;
if (odd_count == 1) {
searchPath(map, odd_char + "", len, ret);
} else{
searchPath(map, "", len, ret);
}
return ret;
} private void searchPath(HashMap<Character, Integer> map, String cur, int target, List<String> ret) {
String new_cur = cur;
int len = new_cur.length();
if (len == target) {
ret.add(new_cur);
return;
}
for (char c : map.keySet()) {
if (map.get(c) >= 2) {
new_cur = c + cur + c;
map.put(c, map.get(c) - 2);
searchPath(map, new_cur, target, ret);
map.put(c, map.get(c) + 2);
}
}
}
}

[LeetCode#267] Palindrome Permutation II的更多相关文章

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

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

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

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

  3. 267. Palindrome Permutation II

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

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

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

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

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

  6. LeetCode Palindrome Permutation II

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

  7. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  8. Palindrome Permutation II 解答

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

  9. [Swift]LeetCode267.回文全排列 II $ Palindrome Permutation II

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

随机推荐

  1. app进入后台申请10分钟活跃时间-b

    IOS允许长时间在后台运行的情况有7种: audio VoIP GPS 下载新闻 和其它附属硬件进行通讯时 使用蓝牙进行通讯时 使用蓝牙共享数据时 除以上情况,程序退出时可能设置短暂运行10分钟 让程 ...

  2. js图片旋转

    <script type="text/javascript" language="javascript"> function rotate(id, ...

  3. 2876: [Noi2012]骑行川藏 - BZOJ

    Description 蛋蛋非常热衷于挑战自我,今年暑假他准备沿川藏线骑着自行车从成都前往拉萨.川藏线的沿途有着非常美丽的风景,但在这一路上也有着很多的艰难险阻,路况变化多端,而蛋蛋的体力十分有限,因 ...

  4. 【BZOJ】【2588】COT(Count On a Tree)

    可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...

  5. uva 11627

    二分 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #inc ...

  6. ural 1864

    题意描述不清   而且还卡精度 ~~ #include <cstdio> #include <cstring> #include <iostream> using ...

  7. httpClient 入门实例

    import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.Unsu ...

  8. linux源代码阅读笔记 free_page_tables()分析

    /* 77 * This function frees a continuos block of page tables, as needed 78 * by 'exit()'. As does co ...

  9. Zabbix 安装及微信短信提醒

    Zabbix简介 Zabbix 近几年得到了各大互联网公司的认可,当然第一点归功与它强大的监控功能,第二点免费开源也得到了广大用户的青睐.Zabbix 能将操作系统中的绝大部分指标进行监控,比如(CP ...

  10. 国内最大的 Node.js 社区将 New Relic 的监控产品换成了 OneAPM

    国内最知名的 CNode 社区把 New Relic 的监控产品换成了 OneAPM .难道 APM 的老大 New Relic 已经被 OneAPM 超越? 毋庸置疑,在全球应用性能管理 SaaS ...