-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeReorder.cpp
More file actions
62 lines (60 loc) · 1.06 KB
/
PalindromeReorder.cpp
File metadata and controls
62 lines (60 loc) · 1.06 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
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 SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileio freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout);
#define ll long long
int main()
{
SPEED;
//fileio;
string s;
cin >> s;
char output[s.length()];
std::map<char, int> m;
ll int count = 0, start = 0 , end = s.length() - 1;
for (ll int i = 0; i < s.length(); i++)
m[s[i]] = m[s[i]] + 1;
for (const auto &i : m)
{
if (i.second % 2)
count += 1;
}
if (count == 0) {
for (const auto &i : m)
{
ll int temp = i.second;
while (temp > 0)
{
temp -= 2;
output[start++] = i.first;
output[end--] = i.first;
}
}
}
else if (count == 1)
{
for (const auto &i : m)
{
ll int temp = i.second;
if (temp % 2)
{
output[(s.length() - 1) / 2] = i.first;
temp--;
}
while (temp > 0 && start != end)
{
temp -= 2;
output[start++] = i.first;
output[end--] = i.first;
}
}
}
else
{
cout << "NO SOLUTION";
return 0;
}
for (char i : output)
cout << i;
return 0;
}