Connection Status:
Competition Arena > AntiPalindrome
TCO07 Sponsor 1 · 2007-03-07 · by Mike Mirzayanov · Simple Math, Simple Search, Iteration, String Manipulation
Class Name: AntiPalindrome
Return Type: String
Method Name: rearrange
Arg Types: (string)
Problem Statement

Problem Statement

A String p is called anti-palindrome if p[i] doesn't equal to p[n - i - 1] for each 0 <= i < (n-1)/2, where n is the length of p. It means that each character (except the middle in the case of a string of odd length) must be different from its symmetric character. For example, "c", "cpp", "java" are anti-palindrome, but "test", "pp" and "weather" are not.

You are given a String s. Rearrange its letters in such a way that the resulting string is anti-palindrome. If there are several solutions, return the one that comes earliest alphabetically. If it is impossible to do it, return the empty string.

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • s will contain only lowercase letters ('a'-'z').
Examples
0)
"test"
Returns: "estt"
1)
"aabbcc"
Returns: "aabcbc"
2)
"reflectionnoitcelfer"
Returns: "cceeeeffiillnnoorrtt"
3)
"hello"
Returns: "ehllo"
4)
"aaaaabbbbbccccc"
Returns: "aaaaabbbccbbccc"

Submissions are judged against all 170 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class AntiPalindrome with a public method string rearrange(string s) · 170 test cases · 2 s / 256 MB per case

Submitting as anonymous