Smallest Palindromic Rearrangement II - Video Solutions
Smallest Palindromic Rearrangement II (Leetcode Weekly 445)
Smallest Palindromic Rearrangement II - Video Solution
Watch 6 video solutions for Smallest Palindromic Rearrangement II, a hard level problem involving Hash Table, Math, String. This walkthrough by Soumya Bhattacharjee has 802 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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". Sincek = 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 = 2exceeds 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". Sincek = 1, the output is"abcba".
Constraints:
1 <= s.length <= 104sconsists of lowercase English letters.sis 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.
Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Generate All Palindromic Permutations | O(n! * n) | O(n!) | Educational approach for understanding palindrome construction; impractical for large inputs |
| Lexicographic Construction with Combinatorics | O(26 * n) | O(26) | Best approach for large strings; counts permutations and skips blocks efficiently |