Trains the technique from
LeetCode 3518Smallest Palindromic Rearrangement IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A tag reads s, and the tag is already the same read either way.
Consider every rearrangement of the tag's letters that also reads the same either way, counting each distinct rearrangement once. List them in dictionary order.
Return the k-th one, or the empty string when fewer than k exist.
Example 1
The front half holds one a and one b, so the halves in order are "ab" and "ba", giving only two rearrangements. There is no fourth, so nothing is returned.
Example 2
The smallest front half is "ab", the middle letter is the odd one out, and mirroring the half gives back the tag itself.
Example 3
The front half holds two a and one b, so the halves in order are "aab", "aba" and "baa"; the third mirrors into this tag.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def smallest_palindrome(s: str, k: int) -> str:public String smallestPalindrome(String s, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.