每日力扣-哈希-回文排列

76 阅读1分钟

给定一个字符串,编写一个函数判定其是否为某个回文串的排列之一。

回文串是指正反两个方向都一样的单词或短语。排列是指字母的重新排列。

回文串不一定是字典当中的单词。

解题思路:hash表存一存,只能有一个是单数才能是回文串

class Solution {
public:
    unordered_map<char,int> hash;
    bool canPermutePalindrome(string s) {
        int flag = 0;
        for(int i = 0 ; i < s.size() ; i++)
        {
            hash[s[i]] ++;
        }
        for(const auto& x : hash)
        {
            if(x.second % 2 != 0)
            {
                if(flag == 0)
                {
                    flag = 1;
                }
                else
                {
                    return false;
                }
            }
        }
        return true;
    }
};