zoukankan      html  css  js  c++  java
  • [LeetCode] Palindrome Permutation II

    Problem Description:

    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.

    As suggested by the first hint, we just need to get one half of the string (each character appears half the times in s), then generate all its unique permutations and concatenate them with the reversed half (possibly the single middle character if the length of the string s is odd).

    All the above work will only be done if an palindrome permutation exists. To tell whether a palindrome permutation exists, Palindrome Permutation has paved the way for us. To generate all the unique permutations, you may as well refer to Permutations II or Next Permutation as suggested by the second hint. But I guess this part is not the main point of this problem, so I directly use the next_permutation of C++. Well, I am not quite whether this is the right way, but this gives shorter codes. Moreover, the tag of this problem is backtracking, which I guess only needs to be used in generating the permutations. After this is done, we can simply concatenate to make the palindromes.

    The code is as follows.

     1 class Solution {
     2 public:
     3     vector<string> generatePalindromes(string s) {
     4         vector<string> palindromes;
     5         unordered_map<char, int> counts;
     6         for (char c : s) counts[c]++;
     7         int odd = 0; char mid; string half;
     8         for (auto p : counts) {
     9             if (p.second & 1) {
    10                 odd++, mid = p.first;
    11                 if (odd > 1) return palindromes;
    12             }
    13             half += string(p.second / 2, p.first);
    14         }
    15         palindromes = permutations(half);
    16         for (string& p : palindromes) {
    17             string t(p);
    18             reverse(t.begin(), t.end());
    19             if (odd) t = mid + t;
    20             p += t;
    21         }
    22         return palindromes;
    23     }
    24 private: 
    25     vector<string> permutations(string& s) {
    26         vector<string> perms;
    27         string t(s);
    28         do {
    29             perms.push_back(s);
    30             next_permutation(s.begin(), s.end());
    31         } while (s != t);
    32         return perms;
    33     }
    34 };
  • 相关阅读:
    ASP.NET 2.0 用户注册控件的密码验证问题
    编程使用GridView,DataList的模版列
    在您的站点上添加 Windows Live Favourites 收藏入口
    推荐个很好玩的开源项目Ascii Generator dotNET
    Castle ActiveRecord 在Web项目和WinForm项目中
    HTML解析器项目进展和新的构思
    SilverLight 的跨域跨域访问
    SQL 语句之Join复习
    【笔记】提高中文分词准确性和效率的方法
    ASP.NET 动态加载控件激发事件的问题
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4752065.html
Copyright © 2011-2022 走看看