leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II

266.Palindrome Permutation

https://www.cnblogs.com/grandyang/p/5223238.html

判断一个字符串的全排列能否形成一个回文串。

能组成回文串,在字符串长度为偶数的情况下,每个字符必须成对出现;奇数的情况下允许一个字符单独出现,其他字符都必须成对出现。用一个set对相同字符进行加减即可。

class Solution {
public:
    /**
     * @param s: the given string
     * @return: if a permutation of the string could form a palindrome
     */
    bool canPermutePalindrome(string &s) {
        // write your code here
        unordered_set<char> container;
        for(auto c : s){
            if(container.find(c) != container.end())
                container.erase(c);
            else
                container.insert(c);
        }
        return container.empty() || container.size() == 1;
    }
};

267.Palindrome Permutation II

猜你喜欢

转载自www.cnblogs.com/ymjyqsx/p/10969920.html
今日推荐