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. WPF 多线程处理(5)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 项目的目录: 以下是FileStroage的 ...

  2. android中设置Animation 动画效果

    在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...

  3. PVPGN 暗黑破坏神2 1.11b战网配置问题汇总

    写了第一篇配置指南之后,很多人向我咨询有关战网搭建的问题.于是觉得很有必要把若干常见的问题,和常用的进阶配置汇总一下,以方便更多人. 1.游戏版本和PVPGN与D2GS版本的问题. PVPGN建议选择 ...

  4. 数据库SQLiteDatabase

    package com.baclock.entity; import android.provider.BaseColumns; /** * Created by Jack on 5/4/2016. ...

  5. UVALive 4872 Underground Cables 最小生成树

    题目链接: 题目 Underground Cables Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %ll ...

  6. [原] perforce 获取本地最近更新的Changelist

    获取perforce客户端最后一次sync的changelist, 前提是中间没有任何代码提交: http://stackoverflow.com/questions/47007/determinin ...

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

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

  8. 如何在Mininet中修改host的IP地址

    how to update virtual host's IP in mininet? I got it! do like this: mininet> py h1.setIP('10.0.0. ...

  9. 李洪强iOS开发之最全App上架流程

    在上架App之前想要 真机测试的同学 请查看 iOS- 最全的真机测试教程 里面包含怎么让多台电脑同时 上架App和同时真机调试.P12文件的使用详解 准备 开发者账号 完工的项目 上架步骤 一.创建 ...

  10. Project Euler 78:Coin partitions

    Coin partitions Let p(n) represent the number of different ways in which n coins can be separated in ...