Skip to main content

Smallest Palindromic Rearrangement II - Solution & Explanation

Practice this problem

Problem Statement

You are given a palindromic string s and an integer k.

Return the k-th lexicographically smallest palindromic permutation of s. If there are fewer than k distinct palindromic permutations, return an empty string.

Note: Different rearrangements that yield the same palindromic string are considered identical and are counted once.

 

Example 1:

Input: s = "abba", k = 2

Output: "baab"

Explanation:

  • The two distinct palindromic rearrangements of "abba" are "abba" and "baab".
  • Lexicographically, "abba" comes before "baab". Since k = 2, the output is "baab".

Example 2:

Input: s = "aa", k = 2

Output: ""

Explanation:

  • There is only one palindromic rearrangement: "aa".
  • The output is an empty string since k = 2 exceeds the number of possible rearrangements.

Example 3:

Input: s = "bacab", k = 1

Output: "abcba"

Explanation:

  • The two distinct palindromic rearrangements of "bacab" are "abcba" and "bacab".
  • Lexicographically, "abcba" comes before "bacab". Since k = 1, the output is "abcba".

 

Constraints:

  • 1 <= s.length <= 104
  • s consists of lowercase English letters.
  • s is guaranteed to be palindromic.
  • 1 <= k <= 106

Approach Overview

Problem Overview: You are given a string and must rearrange its characters to form palindromes. Among all valid palindromic rearrangements, return the k-th lexicographically smallest one. If fewer than k palindromes exist, return an empty string. The challenge is efficiently counting how many palindromes can be formed from the remaining characters while constructing the answer.

Approach 1: Generate All Palindromic Permutations (Brute Force) (Time: O(n! * n), Space: O(n!))

Start by counting character frequencies using a hash table. If more than one character has an odd frequency, forming a palindrome is impossible. Otherwise, build the half-string containing freq[c] / 2 copies of each character. Generate all permutations of this half-string, mirror them around the center character (if one exists), and store the resulting palindromes. After sorting lexicographically, return the k-th palindrome. This approach demonstrates the palindrome construction logic but quickly becomes infeasible because permutations grow factorially.

Approach 2: Lexicographic Construction with Combinatorics (Optimal) (Time: O(26 * n), Space: O(26))

Instead of generating every permutation, compute how many palindromes can be formed from the remaining characters. Count character frequencies with a hash table and build the half-frequency array. The palindrome is determined entirely by the order of this half. Iterate characters from 'a' to 'z' and tentatively place one character in the current position of the half-string. Use combinatorics to calculate how many permutations remain with the updated counts using factorial division: (remaining)! / (freq1! * freq2! ...). If this count is smaller than k, skip those permutations and subtract from k. Otherwise, fix that character and continue building the half. After the half-string is finalized, mirror it and insert the optional middle character to produce the full palindrome. This avoids generating permutations and relies on counting instead.

The core idea is treating palindrome construction as a permutation problem on half the characters. Efficient counting lets you skip entire blocks of permutations and directly construct the desired result.

Recommended for interviews: The combinatorics-based lexicographic construction is the expected approach. Brute force shows you understand how palindromes form, but interviewers typically look for the counting insight that avoids enumerating permutations. Combining string manipulation with combinatorial counting demonstrates strong algorithmic reasoning.

Solution

Code

Rust

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Generate All Palindromic PermutationsO(n! * n)O(n!)Educational approach for understanding palindrome construction; impractical for large inputs
Lexicographic Construction with CombinatoricsO(26 * n)O(26)Best approach for large strings; counts permutations and skips blocks efficiently

Video Solution

Smallest Palindromic Rearrangement II (Leetcode Weekly 445) • Soumya Bhattacharjee • 802 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Smallest Palindromic Rearrangement II easy or hard?
Smallest Palindromic Rearrangement II is classified as Hard because it combines multiple concepts: palindrome feasibility checks, lexicographic ordering, and combinatorial permutation counting. Implementing the counting logic correctly while maintaining efficiency is the main challenge.
Smallest Palindromic Rearrangement II Python/Java solution
Implementations in Python, Java, C++, and Go follow the same pattern: compute character frequencies, build the half-string counts, and iteratively select characters while updating permutation counts. The key part of the code calculates factorial-based combinations to determine how many palindromes remain for each candidate choice.
How to solve Smallest Palindromic Rearrangement II in O(n)?
First count character frequencies and verify that at most one character has an odd count. Build the half-frequency array and construct the half-string one character at a time. At each step, use combinatorial counting to determine how many palindromes are possible if a character is chosen. Skip groups of permutations until the k-th position is reached, then mirror the half to form the final palindrome.
What is the best approach for Smallest Palindromic Rearrangement II?
The best approach uses combinatorics with frequency counting. Count character frequencies, construct the half of the palindrome, and iteratively choose characters in lexicographic order. For each candidate character, compute how many palindromic permutations remain using factorial-based counting. This allows skipping entire groups of permutations and directly building the k-th palindrome.
Is Smallest Palindromic Rearrangement II asked at Google/Amazon/Meta?
Problems combining permutation counting, palindrome construction, and lexicographic ordering appear in interviews at companies like Google, Amazon, and Meta. Variants involving k-th permutation or combinatorial counting are common because they test both mathematical reasoning and efficient string manipulation.
What data structure is used in Smallest Palindromic Rearrangement II?
A hash table or fixed-size frequency array is used to store character counts. The algorithm also relies on factorial values for combinatorial counting. Together these structures allow efficient tracking of remaining characters and calculation of permutation counts.
What is the time complexity of Smallest Palindromic Rearrangement II?
The optimal combinatorics approach runs in O(26 * n) time because each position in the half-string checks at most 26 characters and performs constant-time counting operations. Space complexity is O(26) for storing character frequencies. Brute force permutation generation takes O(n! * n) time and is not practical.

Ready to solve this problem?

Practice Smallest Palindromic Rearrangement II with our built-in code editor and test cases.

Practice on FleetCode