> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Construct K Palindrome Strings Python Solution

> Tested Python solution for LeetCode 1400 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1400, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Greedy](/catalog/topics/greedy), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/construct-k-palindrome-strings/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1400   # by problem number
lcpy gen -s construct_k_palindrome_strings   # by problem name
```

## Problem

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.

### Examples

```
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"`.

```
Input: s = "leetcode", k = 3
Output: false
```

**Explanation:** It is impossible to construct 3 palindromes using all the characters of `s`.

```
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 <= 10^5`
* `s` consists of lowercase English letters.
* `1 <= k <= 10^5`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/construct_k_palindrome_strings/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/construct_k_palindrome_strings/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n)
    # Space: O(1)
    def can_construct(self, s: str, k: int) -> bool:
        odd = sum(count % 2 for count in Counter(s).values())
        return odd <= k <= len(s)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.