1400.cpp (589B)
1 class Solution { 2 public: 3 bool canConstruct(const string &s, int k) { 4 if (s.size() < k) return false; 5 6 int count[27] = {0}; 7 for (const char c : s) 8 count[c & 0x1F]++; 9 10 int singles = 0; 11 for (int i = 1; i <= 26; i++) 12 singles += count[i] & 1; 13 return singles <= k; 14 } 15 }; 16 17 class Solution { 18 public: 19 bool canConstruct(const string &s, int k) { 20 if (s.size() < k) return false; 21 22 bitset<27> bs; 23 for (const char c : s) 24 bs.flip(c & 0x1F); 25 return bs.count() <= k; 26 } 27 };