comments | difficulty | edit_url | tags | ||||
---|---|---|---|---|---|---|---|
true |
Hard |
|
We call a string s
of even length n
an anti-palindrome if for each index 0 <= i < n
, s[i] != s[n - i - 1]
.
Given a string s
, your task is to make s
an anti-palindrome by doing any number of operations (including zero).
In one operation, you can select two characters from s
and swap them.
Return the resulting string. If multiple strings meet the conditions, return the lexicographically smallest one. If it can't be made into an anti-palindrome, return "-1"
.
Example 1:
Input: s = "abca"
Output: "aabc"
Explanation:
"aabc"
is an anti-palindrome string since s[0] != s[3]
and s[1] != s[2]
. Also, it is a rearrangement of "abca"
.
Example 2:
Input: s = "abba"
Output: "aabb"
Explanation:
"aabb"
is an anti-palindrome string since s[0] != s[3]
and s[1] != s[2]
. Also, it is a rearrangement of "abba"
.
Example 3:
Input: s = "cccd"
Output: "-1"
Explanation:
You can see that no matter how you rearrange the characters of "cccd"
, either s[0] == s[3]
or s[1] == s[2]
. So it can not form an anti-palindrome string.
Constraints:
2 <= s.length <= 105
s.length % 2 == 0
s
consists only of lowercase English letters.
The problem asks us to transform the string
Next, we only need to compare whether the two middle characters "1"
. Otherwise, perform the swap operation, move
The time complexity is
class Solution:
def makeAntiPalindrome(self, s: str) -> str:
cs = sorted(s)
n = len(cs)
m = n // 2
if cs[m] == cs[m - 1]:
i = m
while i < n and cs[i] == cs[i - 1]:
i += 1
j = m
while j < n and cs[j] == cs[n - j - 1]:
if i >= n:
return "-1"
cs[i], cs[j] = cs[j], cs[i]
i, j = i + 1, j + 1
return "".join(cs)
class Solution {
public String makeAntiPalindrome(String s) {
char[] cs = s.toCharArray();
Arrays.sort(cs);
int n = cs.length;
int m = n / 2;
if (cs[m] == cs[m - 1]) {
int i = m;
while (i < n && cs[i] == cs[i - 1]) {
++i;
}
for (int j = m; j < n && cs[j] == cs[n - j - 1]; ++i, ++j) {
if (i >= n) {
return "-1";
}
char t = cs[i];
cs[i] = cs[j];
cs[j] = t;
}
}
return new String(cs);
}
}
class Solution {
public:
string makeAntiPalindrome(string s) {
sort(s.begin(), s.end());
int n = s.length();
int m = n / 2;
if (s[m] == s[m - 1]) {
int i = m;
while (i < n && s[i] == s[i - 1]) {
++i;
}
for (int j = m; j < n && s[j] == s[n - j - 1]; ++i, ++j) {
if (i >= n) {
return "-1";
}
swap(s[i], s[j]);
}
}
return s;
}
};
func makeAntiPalindrome(s string) string {
cs := []byte(s)
sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] })
n := len(cs)
m := n / 2
if cs[m] == cs[m-1] {
i := m
for i < n && cs[i] == cs[i-1] {
i++
}
for j := m; j < n && cs[j] == cs[n-j-1]; i, j = i+1, j+1 {
if i >= n {
return "-1"
}
cs[i], cs[j] = cs[j], cs[i]
}
}
return string(cs)
}
function makeAntiPalindrome(s: string): string {
const cs: string[] = s.split('').sort();
const n: number = cs.length;
const m = n >> 1;
if (cs[m] === cs[m - 1]) {
let i = m;
for (; i < n && cs[i] === cs[i - 1]; i++);
for (let j = m; j < n && cs[j] === cs[n - j - 1]; ++i, ++j) {
if (i >= n) {
return '-1';
}
[cs[j], cs[i]] = [cs[i], cs[j]];
}
}
return cs.join('');
}