Construct K Palindrome Strings - Problem

Given a string s and an integer k, determine whether you can use all the characters in s to construct exactly k non-empty palindrome strings.

A palindrome is a string that reads the same forward and backward. Return true if it's possible to construct k palindromes using all characters from s, otherwise return false.

Key constraints:

  • You must use all characters from the input string
  • Each palindrome must be non-empty
  • You need exactly k palindromes

Input & Output

Example 1 — Basic Case
$ Input: s = "annabelle", k = 2
Output: true
💡 Note: We can construct 2 palindromes: "anna" + "elble" or "anna" + "belle". Only one character 'b' has odd frequency, which is ≤ k=2.
Example 2 — Impossible Case
$ Input: s = "leetcode", k = 3
Output: false
💡 Note: Characters l, t, c, o, d all have odd frequencies (1 each). That's 5 odd frequencies > k=3, so impossible.
Example 3 — Edge Case
$ Input: s = "yzyzyzyzyzyzy", k = 2
Output: true
💡 Note: y appears 7 times, z appears 6 times. Only y has odd frequency, so 1 ≤ 2, possible.

Constraints

  • 1 ≤ k ≤ 105
  • 1 ≤ s.length ≤ 105
  • s consists of lowercase English letters

Visualization

Tap to expand
Construct K Palindrome Strings: Input → Process → OutputInputs = "annabelle"k = 2Process: Count Frequenciesa:2, n:2, b:1, e:2, l:2Odd frequencies: 1 (only b)Outputtrue1 ≤ 2Why it works: Each palindrome can have at most 1 character with odd frequencyFor k palindromes, we can handle at most k odd-frequency charactersPossible construction: "anna" (a:2, n:2) + "belle" (b:1, e:2, l:2)
Understanding the Visualization
1
Input
String s and integer k representing desired palindrome count
2
Process
Count character frequencies and identify odd counts
3
Output
True if odd frequencies ≤ k, false otherwise
Key Takeaway
🎯 Key Insight: At most k characters can have odd frequencies for k palindromes
Asked in
Amazon 12 Google 8
28.5K Views
Medium Frequency
~15 min Avg. Time
891 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen