-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeReorder.java
More file actions
31 lines (31 loc) · 1 KB
/
Copy pathPalindromeReorder.java
File metadata and controls
31 lines (31 loc) · 1 KB
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
import java.util.*;
public class PalindromeReorder {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
HashMap<Character,Integer> map=new HashMap<>();
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(map.containsKey(ch))map.put(ch,map.get(ch)+1);
else map.put(ch,1);
}
StringBuilder sb=new StringBuilder();
StringBuilder mid=new StringBuilder();
for(char ch:map.keySet()){
int num = map.get(ch);
int f = num / 2;
int mod = num % 2;
for (int l = 0; l < f; l++) {
sb.append(ch);
}
if (mod == 1) {
mid.append(ch);
if(mid.length()>1){
System.out.println("NO SOLUTION");
return;
}
}
}
System.out.println(sb.toString()+mid.toString()+sb.reverse().toString());
}
}