Palindrome Permutation II
来源:互联网 发布:北京影视大数据公司 编辑:程序博客网 时间:2024/05/16 11:07
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:
- If a palindromic permutation exists, we just need to generate the first half of the string.
- To generate all distinct permutations of a (half of) string, use a similar approach from: Permutations II or Next Permutation.
感觉这类题做多了也就那么回事,只不过在dfs之前的条件需要仔细一些。
在判断是否是回文的时候考的绿naive了一点,用了int[] dict = new int[26], 结果发现输入不局限于a-z。还有就是统计奇数次的字符时,顺便需要更新可能作为中间点的字符。
代码:
public List<String> generatePalindromes(String s) { HashMap<Character, Integer> map = new HashMap<>(); for(int i=0;i<s.length();i++){ if(map.containsKey(s.charAt(i))){ map.put(s.charAt(i), map.get(s.charAt(i))+1); }else{ map.put(s.charAt(i), 1); } } int count = 0; char single = '*'; List<Character> list = new ArrayList<>(); for(Map.Entry<Character, Integer> entry: map.entrySet()){ int value = entry.getValue(); char ch = entry.getKey(); if(value%2!= 0){ count++; single = ch; if(value>1){ for(int i=0;i<(value-1)/2;i++){ list.add(ch); } } }else{ for(int i=0;i<value/2;i++){ list.add(ch); } } } if(count>1){ return new ArrayList<>(); } boolean visited[] = new boolean[list.size()]; List<String> result = new ArrayList<>(); //generate premutations String startStr = single == '*'?"":single+""; int totalLen = single == '*'?2*list.size():2*list.size()+1; generatePermutations(list, startStr, visited, result, totalLen); return result; } private void generatePermutations(List<Character> characters, String curStr, boolean[]visited, List<String> result, int totalLen){ if(curStr.length() == totalLen){ result.add(curStr); return; } for(int i=0;i<characters.size();i++){ if(i!=0 && characters.get(i) == characters.get(i-1) && visited[i-1] == false) continue; if(visited[i] == false){ visited[i] = true; char ch = (characters.get(i)); generatePermutations(characters, ch+curStr+ch, visited, result, totalLen); visited[i] = false; }else{ continue; } } }
0 0
- [LeetCode267]Palindrome Permutation II
- Palindrome Permutation II
- Palindrome Permutation II
- Palindrome Permutation II
- 267. Palindrome Permutation II
- 267. Palindrome Permutation II
- [LeetCode 267] Palindrome Permutation II
- LeetCode 267. Palindrome Permutation II
- LeetCode 267. Palindrome Permutation II
- Leetcode Palindrome Permutation I & II
- LeetCode 题解(242) : Palindrome Permutation II
- [leetcode] 267. Palindrome Permutation II 解题报告
- [Leetcode] 267. Palindrome Permutation II 解题报告
- Palindrome Permutation
- Palindrome Permutation
- Palindrome Permutation
- LeetCode 267. Palindrome Permutation II(对称排列)
- Permutation II
- Oracle分页查询
- A good statement about CAP theory.
- Mac下通过Homebrew安装MySQL 和重置root登录密码
- ICMP报文分析(转)
- 19. Remove Nth Node From End of List 一道简单的双指针
- Palindrome Permutation II
- <caffe学习笔记> caffe训练日志输出
- Notes. Combination -- Batacktrack
- 80老翁谈人生(313):给CSDN创始人与董事长蒋涛的一封公开信
- SpringBoot(1) 使用Intellij IDEA社区版构建SpringBoot项目
- Leetcode 216. Combination Sum III
- 【机器学习】saturate的解释
- LeetCode 359. Logger Rate Limiter 记录速率限制器
- 《C程序设计语言》习题解析 第1章 导言