[LeetCode#267] Palindrome Permutation II
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的更多相关文章
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
- 267. Palindrome Permutation II
题目: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an ...
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- [Swift]LeetCode267.回文全排列 II $ Palindrome Permutation II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
随机推荐
- 分析 "End" "Unload Me" "Exit Sub" 之间的区别与联系
之前就想过这个问题,这么熟悉的几个东西居然对他们分析的不是很透彻. “End” 跟 “Unload Me” 在敲程序 的时候经常敲到,“exit sub” 更是熟悉,下面,解析: End ...
- Most People Aren’t.
Most people want to be fit, most people aren't. Most people want to build a successful business, mos ...
- UI控件tag属性和魔法数字的处理
说明:tag属性有很大的用处,它就好像每个UI控件的id,当多个按钮指向同一个监听方法时,可以给方法带参数UIButton,然后根据不同的tag值 来判断执行哪个按钮的监听事件: - (IBActio ...
- Careercup - Google面试题 - 5735304249999360
2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...
- Useful related java API for Android
Language_suport and Other Language-Oriented API: strings,exceptions, threads, #java.lang.* offers th ...
- myeclipse trial expired[转]
转自:http://blog.csdn.net/yuyuyuyuy/article/details/5878122 今天MyEclipse提示过期了,MyEclipse Trial Expired. ...
- skrollr 中文教程
skrollr 0.6.29 skrollr是一个单独的视差滚动的JavaScript库,移动端(Android,iOS,等)和pc都可以使用,压缩后大小仅仅不到12K 使用方法 首先你需要引入skr ...
- IntelliJ IDEA的Maven项目在修改时报java.lang.OutOfMemoryError: PermGen space异常
什么也不说了---内存溢出,遇见太多回了,下面是解决方式: 1.在项目设置中新建Maven,然后设置VM: 2. 在pom.xml添加下面2个插件,一个是jrebel的,一个是jetty的 <b ...
- JS中的className含义
问题描述: JS中的className含义 问题解决: className说明: className属性可以设置和返回元素的class属性 可以有两种方法来获取对象的c ...
- 你真的知道css三种存在样式(外联样式、内部样式、内联样式)的区别吗?
css样式在html中有三种存在形态: 内联样式:<div style="display: none"></div> 内部样式: <style> ...