Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
Given a string s and an integer k, return true if you can use all the characters in s to construct non-empty k palindrome strings or false otherwise.
Example 1:
Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string.
Constraints:
1 <= s.length <= 105s consists of lowercase English letters.1 <= k <= 105Problem summary: Given a string s and an integer k, return true if you can use all the characters in s to construct non-empty k palindrome strings or false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Greedy
"annabelle" 2
"leetcode" 3
"true" 4
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1400: Construct K Palindrome Strings
class Solution {
public boolean canConstruct(String s, int k) {
int n = s.length();
if (n < k) {
return false;
}
int[] cnt = new int[26];
for (int i = 0; i < n; ++i) {
++cnt[s.charAt(i) - 'a'];
}
int x = 0;
for (int v : cnt) {
x += v & 1;
}
return x <= k;
}
}
// Accepted solution for LeetCode #1400: Construct K Palindrome Strings
func canConstruct(s string, k int) bool {
if len(s) < k {
return false
}
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
x := 0
for _, v := range cnt {
x += v & 1
}
return x <= k
}
# Accepted solution for LeetCode #1400: Construct K Palindrome Strings
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s) < k:
return False
cnt = Counter(s)
return sum(v & 1 for v in cnt.values()) <= k
// Accepted solution for LeetCode #1400: Construct K Palindrome Strings
struct Solution;
impl Solution {
fn can_construct(s: String, k: i32) -> bool {
let k = k as usize;
let n = s.len();
if n < k {
return false;
}
let mut count = vec![0; 26];
for c in s.bytes() {
count[(c - b'a') as usize] += 1;
}
let mut odd = 0;
for v in count {
if v % 2 != 0 {
odd += 1;
}
}
odd <= k
}
}
#[test]
fn test() {
let s = "annabelle".to_string();
let k = 2;
let res = true;
assert_eq!(Solution::can_construct(s, k), res);
let s = "leetcode".to_string();
let k = 3;
let res = false;
assert_eq!(Solution::can_construct(s, k), res);
let s = "true".to_string();
let k = 4;
let res = true;
assert_eq!(Solution::can_construct(s, k), res);
let s = "yzyzyzyzyzyzyzy".to_string();
let k = 2;
let res = true;
assert_eq!(Solution::can_construct(s, k), res);
let s = "cr".to_string();
let k = 7;
let res = false;
assert_eq!(Solution::can_construct(s, k), res);
let s = "aaa".to_string();
let k = 2;
let res = true;
assert_eq!(Solution::can_construct(s, k), res);
}
// Accepted solution for LeetCode #1400: Construct K Palindrome Strings
function canConstruct(s: string, k: number): boolean {
if (s.length < k) {
return false;
}
const cnt: number[] = new Array(26).fill(0);
for (const c of s) {
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
}
let x = 0;
for (const v of cnt) {
x += v & 1;
}
return x <= k;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.