[LeetCode] Construct K Palindrome Strings

1400. Construct K Palindrome Strings

Given a string s and an integer k, return true if you can use all the characters in s to construct k palindrome strings or false otherwise.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool canConstruct(string s, int k) {
if(s.length() < k) return false;
vector<int> freq(26,0);
for(auto& ch : s) freq[ch-'a']++;
int odd = 0;
for(int i = 0; i < 26; i++) {
if(freq[i] & 1) odd++;
}
return odd <= k;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/03/PS/LeetCode/construct-k-palindrome-strings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.