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