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.
Example 1:
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"
Example 2:
Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
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 <= 105
s
consists of lowercase English letters.1 <= k <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to see if a string can be split into a certain number of palindromic parts. The brute force way to do this is to try every possible way to split the string and then check if each part is a palindrome.
Here's how the algorithm would work step-by-step:
def can_construct_k_palindromes_brute_force(input_string, num_palindromes):
string_length = len(input_string)
# If we need more palindromes than the length of the string,
# it's impossible to construct them.
if num_palindromes > string_length:
return False
def is_palindrome(substring):
return substring == substring[::-1]
def can_split_into_palindromes(current_index, palindromes_formed, current_partition):
# Base case: If we've reached the end of the string and
# formed the desired number of palindromes.
if current_index == string_length:
if len(palindromes_formed) == num_palindromes:
return True
else:
return False
# Explore all possible partitions starting from the current index.
for next_index in range(current_index + 1, string_length + 1):
substring = input_string[current_index:next_index]
# Check if the current substring is a palindrome.
if is_palindrome(substring):
# If it is, explore the possibility of including it in our partition.
if can_split_into_palindromes(next_index, palindromes_formed + [substring], current_partition + [substring]):
return True
return False
# Start the recursive process to check if string can be partitioned.
return can_split_into_palindromes(0, [], [])
The key insight is that we can figure out if a solution is even possible by looking at the counts of each letter. If a solution *is* possible, we can simply assign each odd-count character to its own palindrome string. Then we can pad these with pairs of even-count characters to make the required number of strings.
Here's how the algorithm would work step-by-step:
def canConstruct(input_string: str, number_of_palindromes: int) -> bool:
letter_counts = {}
for letter in input_string:
letter_counts[letter] = letter_counts.get(letter, 0) + 1
odd_count_letters = 0
for letter in letter_counts:
if letter_counts[letter] % 2 != 0:
odd_count_letters += 1
# If too many odd counts, impossible to construct.
if odd_count_letters > number_of_palindromes:
return False
# Enough or fewer odd counts means construction is possible.
# Each odd-count character makes its own palindrome.
if odd_count_letters <= number_of_palindromes:
return True
return False
Case | How to Handle |
---|---|
Empty string s and k > 0 | Return false because you cannot create non-empty palindrome strings from an empty string. |
Non-empty string s and k = 0 | Return false because you need to create at least one palindrome string. |
String s has length n and k > n | Return false because you cannot create more palindrome strings than characters in s (each palindrome must have at least one character). |
String s has length n and k = n | Return true if each character in s is unique, as each character can form a palindrome of length 1; otherwise, return false. |
String s has all identical characters and k > 1 | Return false because if all characters are identical, you can only form one palindrome string. |
String s is very long and contains many different characters. | The character count should be done efficiently (O(n) time and O(1) space since alphabet size is constant). |
String s contains unicode characters. | The character count should correctly handle unicode characters using a suitable data structure (e.g., a hash map capable of storing unicode characters as keys). |
k = 1 and s is a non-empty string. | Return true, because any string can be considered as a palindrome of length n. |