【面试题 01.04】回文排列

问题

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

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

示例

输入: "tactcoa"
输出: true(排列有"tacocat"、"atcocta",等等)

解答

class Solution {
public:
    bool canPermutePalindrome(string s) {
        bitset<128> bs;
        for (char ch : s) bs.flip(ch);
        return bs.count() < 2;
    }
};

重点思路

学习C++中bitset的用法(需要#include <bitset>)。

posted @ 2021-08-07 18:40  tmpUser  阅读(21)  评论(0编辑  收藏  举报