556. Make String Anti-palindrome
We call a string s of even length n an anti-palindrome if for every index 0 <= i < n, the condition s[i] != s[n - i - 1] holds.
Given a string s, your task is to rearrange its characters into an anti-palindrome by performing any number of swap operations (including zero).
In one operation, you can select any two characters in s and swap them.
Return the resulting string. If multiple strings satisfy the anti-palindrome condition, return the lexicographically smallest one. If it is impossible to form an anti-palindrome, return "-1".
Example 1:
Input : s = "bdac"
Output : "abcd"
Explanation :
The string "abcd" is an anti-palindrome because:
- s[0] is 'a' and s[3] is 'd' (they are different)
- s[1] is 'b' and s[2] is 'c' (they are different)
- Additionally, "abcd" is the lexicographically smallest valid rearrangement of "bdac".
Example 2:
Input : s = "dada"
Output : "aadd"
Explanation :
- In "aadd", we have:
- s[0] is 'a' and s[3] is 'd'
- s[1] is 'a' and s[2] is 'd'
Since every corresponding pair satisfies the anti-palindrome condition and "aadd" is the lexicographically smallest valid rearrangement of "dada", it is the correct answer.
Now Your Turn!
Pick the correct output for the given inputInput : s = "mmnn"
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 <= s.length <= 105
- s.length is even.
- s consists only of lowercase English letters.