You are given a palindromic string s.
Return the lexicographically smallest palindromic permutation of s.
Example 1:
Input: s = "z"
Output: "z"
Explanation:
A string of only one character is already the lexicographically smallest palindrome.
Example 2:
Input: s = "babab"
Output: "abbba"
Explanation:
Rearranging "babab" → "abbba" gives the smallest lexicographic palindrome.
Example 3:
Input: s = "daccad"
Output: "acddca"
Explanation:
Rearranging "daccad" → "acddca" gives the smallest lexicographic palindrome.
Constraints:
1 <= s.length <= 105s consists of lowercase English letters.s is guaranteed to be palindromic.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 brute force method to find the smallest palindromic rearrangement involves creating all possible arrangements of the input and then checking if each arrangement is a palindrome. We want to find the smallest such palindrome if multiple exist. If no palindrome can be formed, we report failure.
Here's how the algorithm would work step-by-step:
from itertools import permutations
def smallest_palindromic_rearrangement_brute_force(input_string):
smallest_palindrome = None
# Generate all possible permutations of the input string
all_permutations = [''.join(permutation) for permutation in permutations(input_string)]
for current_permutation in all_permutations:
# Check if the current permutation is a palindrome
if current_permutation == current_permutation[::-1]:
#First palindromic permutation found
if smallest_palindrome is None:
smallest_palindrome = current_permutation
#We found a new smaller palindromic permutation
elif current_permutation < smallest_palindrome:
smallest_palindrome = current_permutation
return smallest_palindromeTo find the smallest palindromic rearrangement, we need to determine if a palindrome can even be formed from the given letters. If a palindrome is possible, we construct it by arranging the letters in ascending order from the outside in, mirroring the first half to create the second half.
Here's how the algorithm would work step-by-step:
def find_smallest_palindromic_rearrangement(text):
character_counts = {}
for char in text:
character_counts[char] = character_counts.get(char, 0) + 1
odd_counts = 0
odd_character = ''
for char, count in character_counts.items():
if count % 2 != 0:
odd_counts += 1
odd_character = char
# Palindrome is impossible if more than one char has an odd count.
if odd_counts > 1:
return ''
first_half = ''
# Build the first half of the palindrome using sorted chars.
characters = list(character_counts.keys())
characters.sort()
for char in characters:
even_count = character_counts[char] // 2
first_half += char * even_count
# Place the odd character in the middle if it exists.
middle_character = ''
if odd_character:
middle_character = odd_character * character_counts[odd_character]
# Construct full palindrome by reversing the first half.
second_half = first_half[::-1]
return first_half + middle_character + second_half| Case | How to Handle |
|---|---|
| Empty string input | Return an empty string immediately since an empty string is a palindrome. |
| Null string input | Treat the null input as invalid and return an empty string. |
| String with only one character | Return the original string as a single character is a palindrome. |
| String with two identical characters | Return the original string as it is already a palindrome. |
| String with two different characters that can form a palindrome (e.g., 'ab') | Sort the string alphabetically (e.g., 'ab' becomes 'ab'). |
| String with characters that cannot form a palindrome (more than one character with an odd count) | Return an empty string if the character counts make forming a palindrome impossible. |
| String with a very large number of characters (performance considerations) | Use a character frequency map to optimize the palindrome construction process, ensuring O(n) time complexity. |
| String with all identical characters (e.g., 'aaaa') | Return the original string since it is already the smallest palindrome. |