Talentd
FleetCode

3517. Smallest Palindromic Rearrangement I

Medium74.5% acceptance3 companies

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 <= 105
  • s consists of lowercase English letters.
  • s is guaranteed to be palindromic.

Asked at 3 companies

Hints open one at a time

Hint 1
Consider a palindrome as composed of two mirror-image halves.
Hint 2
Construct one half (using s), and then the other half is its reverse to obtain the lexicographically smallest permutation.
13:07
Still stuck?Read the written solution, or watch a walkthrough.
Open solution

Similar problems

Shortest PalindromeHard
Loading editor…
1class Solution:
2 def smallestPalindrome(self, s: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"z"
Input
"babab"
Input
"daccad"
Your code is saved on this device.Solution