-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(introductory) Palindrome Reorder
64 lines (53 loc) · 1.11 KB
/
(introductory) Palindrome Reorder
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long mod =1e9+7;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
vector<int> alph(26, 0);
string s;
cin>>s;
int n= (int) s.size();
vector<char> ans(n);
for(int i=0; i<n; i++){
alph[s[i]-'A']++;
}
int oddCount =0;
int oddIndex=-1;
for(int i=0; i<26; i++){
if(alph[i]%2!=0){
oddCount++;
oddIndex=i;
}
}
if(((int) s.size() )%2 !=0){
if(oddCount>1){
cout<<"NO SOLUTION"<<endl;
return 0;
}else{
}
}else if(oddCount>0){
cout<<"NO SOLUTION"<<endl;
return 0;
}
ans[(n/2)]=(oddIndex+'A');
alph[oddIndex]--;
int x=0;
int y=n-1;
for(int i=0; i<26; i++){
for(int j=0; j<alph[i]; j++){
if(j%2==0){
ans[x]=(i+'A');
x++;
}else{
ans[y]=(i+'A');
y--;
}
}
}
for(int i=0; i<n; i++){
cout<<ans[i];
}
return 0;
}