Leetcode - 266. Palindrome Permutation

Problem Description:
Given a string, determine if a permutation of the string could form a palindrome.

For example,
“code” -> False, “aab” -> True, “carerac” -> True.
Hint:
Consider the palindromes of odd vs even length. What difference do you notice?
Count the frequency of each character.
If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?


Analysis:
The tricky one :
Increase odds when the increased counter is odd, decrease it otherwise.

bool canPermutePalindrome(string s)
{
    int odd = 0, counts[256] = {0};
    for (char c : s)
        odd += ++counts[c] & 1 ? 1 : -1;
    return odd <= 1;
}

Use bitset.

bool canPermutePalindrome(string s)
{
    bitset<256> b;
    for (char c : s)
        b.flip(c);
    return b.count() < 2;
}

One line python:

def canPermutePalindrome(self, s):
    return sum(v % 2 for v in collections.Counter(s).values()) < 2

Follow up :

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.


ref : //http://www.cnblogs.com/jcliBlogger/p/4752065.html
 #include <iostream>
#include <vector>
#include <string>
using namespace std;
 class Solution {
  public:
     vector<string> generatePalindromes(string s) {
     vector<string> res;
    // unordered_map<char, int> counter;
     int count[256] = {0};
     int odd = 0; 
     char mid; 
     string half;
     for (char c : s)
            odd += ++count[c] & 1 ? 1 : -1;
     if(odd >= 2) return res;
     for (int i = 0; i < 256; i++)
     {
        if(count[i] & 1)
            mid = char(i);
            half += string(count[i] / 2, char(i));
     }
     helper (res, half, mid, 0);
     return res;
     }
     void helper (vector<string> & res, string s, char mid, int begin)
     {
        if (begin >= s.size())
        {
        string t = s;
        reverse(t.begin(), t.end());
        t += mid;
        t += s;
        res.push_back(t); 
        return ; 
        }

        for (int i = begin; i < s.size(); ++i)
        {
            if (i == begin || s[i] != s[i - 1])
                {
                swap(s[i], s[begin]);
                helper(res, s, mid, begin + 1);
                }
        }

     }
 };
 int main(int argc, char const *argv[])
 {
    Solution s;
    string x = "abcccba";
    vector<string> v = s.generatePalindromes(x);
    for (std::vector<string>::iterator i = v.begin(); i != v.end(); ++i)
    {
        cout<< *i <<endl;
    }
    return 0;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值