-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpalindromereorder.cpp
More file actions
39 lines (34 loc) · 889 Bytes
/
palindromereorder.cpp
File metadata and controls
39 lines (34 loc) · 889 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
vector<int> a(26);
for(char c : s) a[c - 'A']++;
int check = 0;
for(int i = 0; i < 26; i++) check += (a[i] % 2);
//only one of the letters should be of odd frequency or all pair
if(check > 1){
cout << "NO SOLUTION";
return 0;
}
//organize the palindrome
string result;
for (int i = 0; i < 26; i++){
if(!(a[i]%2)){
for(int j = 0; j < a[i]/2; j++) result.push_back(i + 'A');
}
}
for (int i = 0; i < 26; i++){
if(a[i]%2){
for(int j = 0; j < a[i]; j++) result.push_back(i + 'A');
}
}
for (int i = 25; i >= 0; i--){
if(!(a[i]%2)){
for(int j = 0; j < a[i]/2; j++) result.push_back(i + 'A');
}
}
cout << result << endl;
return 0;
}