Skip to main content
Back to problems
Leetcode
Medium
Arrays
Strings
Greedy
Sorting
Google
3518. Smallest Palindromic Rearrangement II

Rearrange the characters of a string to form the lexicographically smallest palindrome, if one can be formed.

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

Smallest Palindromic Rearrangement II

gfg
Problem Statement

Problem

Given a string s, rearrange all of its characters to form a palindrome that is lexicographically smallest among all possible palindromic rearrangements.

If no palindrome can be formed using all characters of s, return an empty string.

A string is lexicographically smaller than another string if at the first position where they differ, the character in the first string comes earlier in alphabetical order.

Notes

  • You must use every character exactly once.
  • The resulting string must read the same forward and backward.
  • If multiple palindromic rearrangements exist, choose the smallest one in lexicographic order.

Input Format

  • A single string s consisting of lowercase English letters.

Output Format

  • Return the lexicographically smallest palindrome that can be formed using all characters of s.
  • If impossible, return "".

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 smaller one is abba.

Example 2

Input

s = "bbaa"

Output

"abba"

Explanation

The character counts are the same as in the first example, so the smallest palindromic rearrangement is still abba.

Show 1 more example

Example 3

Input

s = "abc"

Output

""

Explanation

Each character appears once, so more than one character has an odd frequency. 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.