You are given two strings s and target, each of length n, consisting of lowercase English letters.
Return the lexicographically smallest string that is both a palindromicpermutation of s and strictly greater than target. If no such permutation exists, return an empty string.
Example 1:
Input:s = "baba", target = "abba"
Output:"baab"
Explanation:
The palindromic permutations of s (in lexicographical order) are "abba" and "baab".
The lexicographically smallest permutation that is strictly greater than target is "baab".
Example 2:
Input:s = "baba", target = "bbaa"
Output:""
Explanation:
The palindromic permutations of s (in lexicographical order) are "abba" and "baab".
None of them is lexicographically strictly greater than target. Therefore, the answer is "".
Example 3:
Input:s = "abc", target = "abb"
Output:""
Explanation:
s has no palindromic permutations. Therefore, the answer is "".
Example 4:
Input:s = "aac", target = "abb"
Output:"aca"
Explanation:
The only palindromic permutation of s is "aca".
"aca" is strictly greater than target. Therefore, the answer is "aca".
Constraints:
1 <= n == s.length == target.length <= 300
s and target consist of only lowercase English letters.
Solutions
Solution 1
Thinking
A palindromic permutation is determined by its left half and at most one odd center; more than one odd frequency is impossible. We want the smallest palindrome strictly larger than \(\textit{target}\), so the left half is built like the next permutation: match the first half of \(\textit{target}\) as far as possible, raise the first feasible position, and mirror the left half to the right.