Construct K Palindrome Strings

Given a string `s` and an integer `k`, return true if you can use all the characters in `s` to build exactly `k` non-empty palindrome strings, and false otherwise. Each character of `s` must be used exactly once.

30:00

Construct K Palindrome Strings
medium
Free written solution
Topics
Companies

Given a string s and an integer k, return true if you can use all the characters in s to build exactly k non-empty palindrome strings, and false otherwise. Each character of s must be used exactly once.

Example 1:
Input: {"s":"annabelle","k":2}
Output: true
Constraints:
  • 1 <= s.length <= 10^5

  • s consists of lowercase English letters.

  • 1 <= k <= 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"annabelle","k":2}

We must split 9 letters into exactly 2 non-empty palindromes. First, each palindrome needs at least one letter, so k cannot exceed 9.

s

counted 0/9 · k = 2

a
n
n
a
b
e
l
l
e

letter buckets: pairs + leftover

a

0

b

0

e

0

l

0

n

0

pair (goes on both sides)

leftover (needs a center)

Variables
VariableValue
n9
k2
DepthFunction Call
Stack empty
0/17