Letters Rearranging
Hazrat Ali
You can rearrange all letters of this string as you wish. Your task is to obtain a good string by rearranging the letters of the given string or report that it is impossible to do it.
Let's call a string good if it is not a palindrome. Palindrome is a string which is read from left to right the same as from right to left. For example, strings "abacaba", "aa" and "z" are palindromes and strings "bba", "xd" are not.
You have to answer tt independent queries.
The first line of the input contains one integer tt (1≤t≤100) — number of queries.
Each of the next tt lines contains one string. The ii-th line contains a string sisi consisting only of lowercase Latin letter. It is guaranteed that the length of si is from 1 to 1000 (inclusive).
Print tt lines. In the ii-th line print the answer to the ii-th query: -1 if it is impossible to obtain a good string by rearranging the letters of sisi and any good string which can be obtained from the given one (by rearranging the letters) otherwise.
3 aa abacaba xdd
-1 abaacba xdd
Solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
string s;
cin >> s;
sort(s.begin(), s.end());
string ans = s.front() == s.back() ? "-1" : s;
cout << ans << endl;
}
return 0;
}