Construct K Palindrome Strings - Problem

Imagine you're a word puzzle master tasked with creating k palindrome strings using all the characters from a given string s. A palindrome reads the same forwards and backwards, like "racecar" or "level".

Your challenge is to determine if it's possible to redistribute all characters from the input string to form exactly k non-empty palindromes. Each character must be used exactly once, and you need to create exactly k palindromic strings.

Examples:

  • With string "annabelle" and k=2, you could form ["anna", "belleb"]
  • With string "abc" and k=3, you could form ["a", "b", "c"] (single characters are palindromes)
  • With string "abc" and k=2, it's impossible since we'd need at least one multi-character palindrome, but we only have unique characters

Return true if the redistribution is possible, false otherwise.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "annabelle", k = 2
โ€บ Output: true
๐Ÿ’ก Note: We can construct 2 palindromes: "anna" (using a,n,n,a) and "belleb" (using b,e,l,l,e,b). Character frequencies: a:2, n:2, b:1, e:2, l:2. Only 'b' has odd frequency (1), and 1 โ‰ค 2, so it's possible.
example_2.py โ€” Impossible Case
$ Input: s = "leetcode", k = 3
โ€บ Output: false
๐Ÿ’ก Note: Character frequencies: l:1, e:3, t:1, c:1, o:1, d:1. We have 5 characters with odd frequencies, but k=3. Since each palindrome can have at most 1 odd-frequency character, we need at least 5 palindromes, not 3.
example_3.py โ€” Edge Case - All Singles
$ Input: s = "abc", k = 3
โ€บ Output: true
๐Ÿ’ก Note: We can form 3 single-character palindromes: "a", "b", "c". Each character appears once (odd frequency), and we have exactly 3 odd-frequency characters for k=3 palindromes.

Visualization

Tap to expand
๐Ÿ—๏ธ Building Palindromes: The Construction AnalogyExample: s="aabbce", k=2 โ†’ Can we build 2 palindromic buildings?Step 1: Inventory Count ๐Ÿ“ฆa:2b:2c:1e:1โœ“ Paired materials (even count): a, bโš ๏ธ Unpaired materials (odd count): c, e โ†’ Need center positionsStep 2: Building Rules ๐Ÿ—๏ธโ€ข Each palindromic building needs symmetry: left side = right side (mirrored)โ€ข Paired materials can be split between left and right sidesโ€ข Each building can have AT MOST 1 unpaired material in the center๐Ÿ”‘ Key: If unpaired_count โ‰ค k, we can assign each unpaired to a different building!Step 3: Construction Plan โœ…Building 1: "abcba" (using a:2, b:2, c:1)Building 2: "e" (using e:1)Result: 2 unpaired โ‰ค 2 buildings โ†’ TRUE!
Understanding the Visualization
1
Count Materials
Count how many of each character (building material) we have
2
Find Unpaired Items
Characters with odd counts are 'unpaired' - they need to go in the center of palindromes
3
Check Distribution
Since each palindrome can have at most one center piece, we need odd_count โ‰ค k
Key Takeaway
๐ŸŽฏ Key Insight: Just like each symmetric building can have at most one unique centerpiece, each palindrome can contain at most one character with an odd frequency. The solution elegantly reduces to: count odd-frequency characters and check if โ‰ค k.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string to count character frequencies

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only need space for at most 26 character counts (constant space)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค k โ‰ค s.length โ‰ค 105
  • s consists of lowercase English letters only
  • Each palindrome must be non-empty
  • All characters from s must be used exactly once
Asked in
Amazon 35 Google 28 Meta 22 Microsoft 18
52.8K Views
Medium Frequency
~15 min Avg. Time
2.3K 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