๐ฏ The Anti-Palindrome Challenge
In the world of string manipulation, we often encounter palindromes - strings that read the same forwards and backwards. But what about their opposite? An anti-palindrome is a string where every character at position i is different from its mirror character at position n-i-1.
Given a string s of even length, your mission is to transform it into an anti-palindrome through any number of character swaps. The twist? Among all possible anti-palindromes you can create, return the lexicographically smallest one.
Example: For string "abab", positions 0 and 3 have the same character 'a', and positions 1 and 2 have the same character 'b'. We need to rearrange so that s[0] โ s[3] and s[1] โ s[2].
If it's impossible to create an anti-palindrome, return "-1".
Input & Output
Time & Space Complexity
O(n) for counting + O(n log n) for sorting characters
Only constant space for character frequency array (26 letters)
Constraints
- 1 โค s.length โค 105
- s.length is even
- s consists of lowercase English letters only
- Important: Anti-palindrome requires s[i] โ s[n-1-i] for ALL positions 0 โค i < n/2