Smallest Palindromic Rearrangement II - Problem

Given a palindromic string s and an integer k, your task is to find the k-th lexicographically smallest palindromic permutation of the characters in s.

Key points:

  • The input string s is already a palindrome
  • You need to rearrange its characters to form different palindromes
  • Return the k-th smallest palindrome in lexicographical order
  • If there are fewer than k distinct palindromic permutations, return an empty string
  • Different arrangements that create the same palindromic string count as one permutation

Example: For s = "aab" and k = 2, the palindromic permutations are ["aba", "baa"], so return "baa".

Input & Output

example_1.py — Basic Case
$ Input: s = "aab", k = 2
Output: "baa"
💡 Note: The palindromic permutations of "aab" are ["aba", "baa"] in lexicographical order. The 2nd one is "baa".
example_2.py — Single Character
$ Input: s = "aaa", k = 1
Output: "aaa"
💡 Note: Only one palindromic permutation exists: "aaa". Since k=1, return "aaa".
example_3.py — Impossible Case
$ Input: s = "abc", k = 1
Output: ""
💡 Note: "abc" cannot form any palindromic permutation because all characters appear with odd frequency (more than one odd frequency character).

Constraints

  • 1 ≤ s.length ≤ 16
  • s consists of only lowercase English letters
  • s is a palindromic string
  • 1 ≤ k ≤ 109
  • The input string s is guaranteed to be a palindrome

Visualization

Tap to expand
Palindrome Construction VisualizationExample: s = "aabbcd", k = 2Frequencies: a:2, b:2, c:1, d:1 → Two odd frequencies!❌ Cannot form palindrome (more than 1 odd frequency character)Result: Return empty string ""Valid Example: s = "aabbc", k = 1Frequencies: a:2, b:2, c:1 → Only one odd frequency ✓Middle character: 'c', Half frequencies: a:1, b:1First half possibilities: "ab" or "ba"Palindromes: "abcba", "bacab"Sorted: ["abcba", "bacab"]k=1 → Return "abcba"Palindrome StructureabcbaFirst Half | Mid | Second HalfMirrorMirrorCombinatorial Ranking ProcessFor first position of first half:• If we choose 'a': remaining = {b:1} → 1 permutation possible• If we choose 'b': remaining = {a:1} → 1 permutation possibleSince k=1 ≤ 1: Choose 'a', build first half = "ab"Final palindrome: "ab" + "c" + "ba" = "abcba"🎯 Time: O(n), Space: O(1) - No permutation generation needed!
Understanding the Visualization
1
Analyze Structure
Count character frequencies. Characters with odd counts go in middle, others are split symmetrically
2
Build First Half
Use combinatorics to determine which character goes at each position for the k-th permutation
3
Complete Palindrome
Construct: first_half + middle_character + reverse(first_half)
Key Takeaway
🎯 Key Insight: Palindromes have inherent symmetry - we only need to determine the first half and middle character. Using combinatorial mathematics, we can directly calculate which characters belong in each position for the k-th palindrome without generating all possible permutations.
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
28.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen