Given a string s
and an integer k
, determine if it is possible to construct k
non-empty palindrome strings using all the characters in s
exactly once. Each character must be used in exactly one palindrome, and each palindrome must be non-empty. You may rearrange the characters in s
as needed.
true
if it is possible to construct exactly k
palindrome strings as described, otherwise return false
.s
must be used exactly once.
To solve this problem, we need to understand what makes a string a palindrome and how to partition a set of characters into palindromes. The brute-force way would be to try every possible partition of the characters into k
groups and check if each group forms a palindrome, but this is computationally infeasible for larger strings.
Instead, we notice that the key property of a palindrome is that at most one character can have an odd count (for odd-length palindromes), and all others must have even counts. For k
palindromes, the minimum number of palindromes we need is at least the number of characters with odd counts, because each such character must be the center of some palindrome.
So, the problem reduces to counting the number of characters with odd frequencies, and checking whether it's possible to create k
palindromes given this constraint.
s
. This can be done using a hash map or array.odd_count
.odd_count
palindromes, because each odd-count character must be the center of a palindrome.k
must be less than or equal to len(s)
.odd_count ≤ k ≤ len(s)
, return true
; otherwise, return false
.We use a hash map for counting because it allows O(1) lookups and updates for each character. This makes the solution efficient and straightforward.
Example: s = "annabelle"
, k = 2
odd_count = 1
).odd_count = 1
k = 2
len(s) = 8
Since odd_count ≤ k ≤ len(s)
(1 ≤ 2 ≤ 8), it is possible to construct 2 palindrome strings using all characters.
Another Example: s = "leetcode"
, k = 3
Frequencies: l:1, e:3, t:1, c:1, o:1, d:1
Odd-count characters: l, t, c, o, d (each 1), e (3): so 6 odd-count characters.
Since odd_count = 6
and k = 3
, and 6 > 3, it is not possible.
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s):
return False
from collections import Counter
freq = Counter(s)
odd_count = sum(1 for v in freq.values() if v % 2 == 1)
return odd_count <= k
class Solution {
public:
bool canConstruct(string s, int k) {
if (k > s.size()) return false;
vector<int> freq(26, 0);
for (char c : s) freq[c - 'a']++;
int odd_count = 0;
for (int f : freq) {
if (f % 2 == 1) odd_count++;
}
return odd_count <= k;
}
};
class Solution {
public boolean canConstruct(String s, int k) {
if (k > s.length()) return false;
int[] freq = new int[26];
for (char c : s.toCharArray()) freq[c - 'a']++;
int odd_count = 0;
for (int f : freq) {
if (f % 2 == 1) odd_count++;
}
return odd_count <= k;
}
}
var canConstruct = function(s, k) {
if (k > s.length) return false;
let freq = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) {
freq[s.charCodeAt(i) - 97]++;
}
let odd_count = 0;
for (let f of freq) {
if (f % 2 === 1) odd_count++;
}
return odd_count <= k;
};
s
. We scan the string once to count frequencies, and then once more to count odd frequencies.
The key insight is that the minimum number of palindromes needed is equal to the number of characters with odd frequencies, since each odd-count character must be the center of a palindrome. By counting the odd frequencies and checking the constraints, we can efficiently determine if k
palindrome strings can be constructed from the input. This solution is both elegant and efficient, leveraging fundamental properties of palindromes and character counts.