Smallest Palindromic Rearrangement II Problem
Description
LeetCode Problem 3518.
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 <= 10^4sconsists of lowercase English letters.sis guaranteed to be palindromic.1 <= k <= 10^6
Solution
Approach: Counting Permutations and Building the k-th One
A palindrome is determined by its first half, so we need the k-th smallest permutation of the multiset of half-counts (each letter count divided by 2). If more than one letter has an odd count, there is no palindrome at all, so return an empty string.
Let perms(counts) be the number of distinct arrangements of a multiset, computed as a product of binomial coefficients and capped at a limit just above the maximum k (106), so nothing overflows. If k exceeds perms(half), return an empty string.
Otherwise build the half position by position. For each position, try the letters from a to z: take the letter, count the arrangements of what remains, and if that number is at least the remaining rank k, keep the letter; otherwise subtract it from k, put the letter back and try the next one. Finally mirror the half around the optional middle letter.
Complexity: O(n * 26 * 26 * C) time in the worst case, where C is the cap, although the binomial loops stop early in practice; O(1) extra space besides the output.
class Solution:
def smallestPalindrome(self, s: str, k: int) -> str:
limit = 1_000_001
freq = [0] * 26
for ch in s:
freq[ord(ch) - 97] += 1
odd = 0
mid = 26
for i, v in enumerate(freq):
if v & 1:
odd += 1
mid = i
if odd > 1:
return ""
half = [v // 2 for v in freq]
half_len = sum(half)
def count_permutations(counts: List[int]) -> int:
remaining = sum(counts)
perms = 1
for count in counts:
if count == 0:
continue
selected = min(count, remaining - count)
combos = 1
for step in range(1, selected + 1):
combos = combos * (remaining - step + 1) // step
if combos >= limit:
combos = limit
break
perms *= combos
if perms >= limit:
return limit
remaining -= count
return perms
if k > count_permutations(half):
return ""
n = len(s)
pal = [""] * n
rank = k
pos = 0
for _ in range(half_len):
for i in range(26):
if half[i] == 0:
continue
half[i] -= 1
suffix = count_permutations(half)
if suffix >= rank:
pal[pos] = chr(97 + i)
pos += 1
break
rank -= suffix
half[i] += 1
if mid < 26:
pal[half_len] = chr(97 + mid)
for i in range(half_len):
pal[n - 1 - i] = pal[i]
return "".join(pal)