Rearrange the characters of a string to form the lexicographically smallest palindrome, if possible.
Smallest Palindromic Rearrangement I
gfgSmallest Palindromic Rearrangement I
Given a string, rearrange all of its characters to form a palindrome. If multiple palindromic rearrangements are possible, return the lexicographically smallest one.
A palindrome reads the same from left to right and right to left.
If no palindromic rearrangement exists, return an empty string.
You must use every character exactly as many times as it appears in the input.
Input Format
- A string
sconsisting of lowercase English letters.
Output Format
- Return the lexicographically smallest palindrome that can be formed using all characters of
sexactly once each. - If it is impossible to form any palindrome, return an empty string.
Constraints
scontains only lowercase English letters.
Example 1
Input
s = "aabb"
Output
"abba"
Explanation
Two palindromes are possible: "abba" and "baab". The lexicographically smallest is "abba".
Example 2
Input
s = "bbaa"
Output
"abba"
Explanation
The same multiset of characters can be rearranged into the smallest palindrome "abba".
Show 1 more example
Example 3
Input
s = "abc"
Output
""
Explanation
Each character appears once, so there are three odd counts. No palindrome can be formed.
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.