Problem Description
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.
Key Insights
- Count the frequency of each character in the string.
- A palindrome can have at most one character with an odd frequency.
- Each palindrome string can incorporate one odd frequency character.
- The minimum number of palindromes required equals the number of odd frequency characters.
- Additionally, k must not exceed the length of s since each palindrome should be non-empty.
Space and Time Complexity
Time Complexity: O(n), where n is the length of s. Space Complexity: O(1), since we only count characters from a fixed alphabet (26 lowercase letters).
Solution
The approach is to first count the frequency of each character in the string using a hash table (or dictionary). Then, we determine the number of characters that appear an odd number of times, as each palindrome can accommodate one odd count character. If the number of odd frequency characters is less than or equal to k and k is also less than or equal to the length of s, then it is possible to rearrange s into k palindrome strings. Otherwise, it is not possible.