zoukankan      html  css  js  c++  java
  • Leetcode: 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.
    
    For example:
    
    Given s = "aabb", return ["abba", "baab"].
    
    Given s = "abc", return [].

    Hint:

    1. If a palindromic permutation exists, we just need to generate the first half of the string.
    2. To generate all distinct permutations of a (half of) string, use a similar approach from: Permutations II or Next Permutation.
     1 public class Solution {
     2     public List<String> generatePalindromes(String s) {
     3         List<String> res = new ArrayList<String>();
     4         List<String> result = new ArrayList<String>();
     5         if (s==null || s.length()==0) return res;
     6         int[] countChar = new int[256];
     7         for (int i=0; i<s.length(); i++) {
     8             char c = s.charAt(i);
     9             countChar[(int)(c-'')]++;
    10         }
    11         ArrayList<Character> evenHalf = new ArrayList<Character>();
    12         char oddChar = '';
    13         int countOdd = 0;
    14         for (int i=0; i<256; i++) {
    15             char c = (char)(i+'');
    16             int cNum = countChar[i];
    17             if (cNum>0 && cNum%2==0) { //c appears even times
    18                 for (int ii=1; ii<=cNum/2; ii++) {
    19                     evenHalf.add(c);
    20                 }
    21             }
    22             else if (cNum%2 == 1) {//c appears odd times
    23                 for (int ii=1; ii<=cNum/2; ii++) {
    24                     evenHalf.add(c);
    25                 }
    26                 oddChar = c;
    27                 countOdd++;
    28             }
    29         }
    30         if (s.length()%2==0 && countOdd!=0) return res;
    31         if (s.length()%2==1 && countOdd!=1) return res;
    32         ArrayList<Integer> visited = new ArrayList<Integer>();
    33         halfPermute(res, "", evenHalf, visited);
    34         for (String each : res) {
    35             String rev = new StringBuffer(each).reverse().toString();
    36             if (s.length()%2==0) result.add(each + rev);
    37             else result.add(each + oddChar + rev);
    38         }
    39         return result;
    40     }
    41     
    42     public void halfPermute(List<String> res, String item, ArrayList<Character> evenHalf, ArrayList<Integer> visited) {
    43         if (item.length()==evenHalf.size()) {
    44             res.add(item);
    45             return;
    46         }
    47         for (int i=0; i<evenHalf.size(); i++) {
    48             if (i>0 && evenHalf.get(i)==evenHalf.get(i-1) && !visited.contains(i-1)) continue;
    49             if (!visited.contains(i)) {
    50                 visited.add(i);
    51                 halfPermute(res, item+evenHalf.get(i), evenHalf, visited);
    52                 visited.remove(visited.size()-1);
    53             }
    54         }
    55     }
    56 }
  • 相关阅读:
    tornado硬件管理系统-数据存储与表格实时监控(8)
    db2 内存研究
    Oracle dml开始到commit期间的流程
    用户界面与业务逻辑的分离
    计算器核心算法——终结版
    计算器核心算法——中缀表达式转为后缀表达式
    计算器核心解析算法(上)
    Qt中的字符串类
    初探Qt中的消息处理
    计算器界面代码重构
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5069573.html
Copyright © 2011-2022 走看看