Skip to main content
Back to problems
Leetcode
Medium
Hash Maps
Strings
Greedy
Construct K Palindrome Strings

Determine whether a string can be rearranged into exactly kk palindrome strings.

Acceptance 0%
Problem Statement

Given a string ss, you may rearrange its characters any way you like. Decide whether it is possible to split all characters of ss into exactly kk non-empty strings such that every string is a palindrome.

A palindrome reads the same forward and backward. Every character of ss must be used exactly once in the kk strings.

Input Format

  • A string s
  • An integer k

Output Format

  • Return true if the characters of s can be rearranged into exactly k palindrome strings.
  • Otherwise, return false.

Constraints

  • 1s1051 \le |s| \le 10^5
  • s contains lowercase English letters
  • 1ks1 \le k \le |s|
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "annabelle", k = 2

Output

true

Explanation

One valid rearrangement is "anna" + "belle"? No, that second string is not a palindrome. A valid split after rearranging is "anna" and "belleb"? That uses too many characters. Instead, the characters can be rearranged into "anna" and "eblleb"; both are palindromes and use all letters exactly once.

Example 2

Input

s = "leetcode", k = 3

Output

false

Explanation

The string has too many characters with odd frequencies to form 3 palindrome strings after rearrangement.

Show 1 more example

Example 3

Input

s = "true", k = 4

Output

true

Explanation

Each character can form its own single-letter palindrome, so 4 palindromes are possible.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.