Make String Anti-palindrome - Problem

๐ŸŽฏ 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

example_1.py โ€” Basic Anti-palindrome
$ Input: s = "abab"
โ€บ Output: "abba"
๐Ÿ’ก Note: We need to ensure s[0] โ‰  s[3] and s[1] โ‰  s[2]. The original has 'a' at positions 0,3 and 'b' at positions 1,2. By swapping to get 'abba', we have s[0]='a' โ‰  s[3]='a' becomes s[0]='a' โ‰  s[3]='a'... wait, we need 'abba' where s[0]='a' โ‰  s[3]='a' is false. Let me correct: 'abba' has s[1]='b' โ‰  s[2]='b' which is also false. The correct answer should be a rearrangement like 'abab' โ†’ 'aabb' won't work either. Actually 'baab' works: s[0]='b' โ‰  s[3]='b'... I need to recalculate. For anti-palindrome 'abab' โ†’ 'baab' doesn't work. Let's try 'abab' โ†’ 'acbc' but we don't have 'c'. The answer 'abba' means: s[0]='a' โ‰  s[3]='a' (FALSE), so this is wrong. Let me reconsider... For 'abab' we need a rearrangement where position 0 โ‰  position 3 AND position 1 โ‰  position 2. If we have characters a,b,a,b, one valid anti-palindrome could be 'aabb' where s[0]='a' โ‰  s[3]='b' โœ“ and s[1]='a' โ‰  s[2]='b' โœ“. So the lexicographically smallest would be 'aabb'.
example_2.py โ€” Impossible Case
$ Input: s = "aaaa"
โ€บ Output: "-1"
๐Ÿ’ก Note: All characters are the same, so it's impossible to create an anti-palindrome where s[i] โ‰  s[n-1-i] for any position i. Since every position would have the same character as its mirror position, we return "-1".
example_3.py โ€” Complex Case
$ Input: s = "aabbcc"
โ€บ Output: "abcacb"
๐Ÿ’ก Note: With 6 characters, we need s[0] โ‰  s[5], s[1] โ‰  s[4], s[2] โ‰  s[3]. We have 2 a's, 2 b's, 2 c's. One optimal arrangement is 'abcacb' where s[0]='a' โ‰  s[5]='b', s[1]='b' โ‰  s[4]='c', s[2]='c' โ‰  s[3]='a'. This satisfies anti-palindrome property and is lexicographically minimal.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

O(n) for counting + O(n log n) for sorting characters

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only constant space for character frequency array (26 letters)

n
2n
โœ“ Linear Space

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
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen