Construct K Palindrome Strings(C++构造 K 个回文字符串)

解题思路:

(1)数学思想,抽屉原理

class Solution {
public:
    bool canConstruct(string s, int k) {
        if(s.length()<k) return false;
        unordered_map<char,int> mp;
        for(int i=0;i<s.length();i++) {
            mp[s[i]]++;
        }
        
        int count=0;
        for(auto it=mp.begin();it!=mp.end();it++) {
            if(it->second%2==1) count++;
        }
        if(count>k) return false;
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/114696674
k