Skip to main content
Back to problems
Leetcode
Medium
Strings
Hash Maps
Greedy
3517. Smallest Palindromic Rearrangement I

Rearrange the characters of a string to form the lexicographically smallest palindrome, if possible.

Acceptance 100%
Also Available On
Other platform versions and source mappings for the same problem.

Smallest Palindromic Rearrangement I

gfg
Problem Statement

Smallest 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 s consisting of lowercase English letters.

Output Format

  • Return the lexicographically smallest palindrome that can be formed using all characters of s exactly once each.
  • If it is impossible to form any palindrome, return an empty string.

Constraints

  • 1s1051 \le |s| \le 10^5
  • s contains only lowercase English letters.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.