-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
46 lines (43 loc) · 1.25 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.*;
import java.io.*;
public class Solution {
private static boolean isPalindrome(char[] chars) {
int n = chars.length;
for (int i = 0; i < n / 2; i++) {
if (chars[i] != chars[n - i - 1])
return false;
}
return true;
}
private static char[] nonPalindrome(char[] str) {
int n = str.length;
char[] frequency = new char[128];
for (int i = 0; i < n; i++) {
frequency[str[i]]++;
}
for (int i = 0, k = 0; i < 128; i++) {
if (frequency[i] != 0) {
for (int j = 1; j <= frequency[i]; j++) {
str[k++] = (char) i;
}
}
}
return str;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 1; i <= t; i++) {
String istr = in.next();
char[] str = nonPalindrome(istr.toCharArray());
if (isPalindrome(str))
System.out.print(-1);
else {
for (int j = 0; j < str.length; j++) {
System.out.print(str[j]);
}
}
System.out.println();
}
}
}