CSES Solutions
#1755Introductory Problems

Palindrome Reorder

View on CSES

Solution

1755-Palindrome_Reorder.cpp
1#include<bits/stdc++.h>
2using namespace std;
3#define int long long
4signed main(){
5  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
6  string s; cin>>s;
7  int cnt[26]{};
8  for(auto&c:s) ++cnt[c-'A'];
9  int o=0;
10  for(auto&x:cnt) o+=x&1;
11  if(o>1) cout<<"NO SOLUTION\n";
12  else{
13    string ans="";
14    for(int i=0;i<26;++i) if(cnt[i]&1^1) for(int j=0;j<cnt[i]>>1;++j)
15      ans+=(char)('A'+i);
16    cout<<ans;
17    for(int i=0;i<26;++i) if(cnt[i]&1) for(int j=0;j<cnt[i];++j)
18      cout<<(char)('A'+i);
19    reverse(ans.begin(),ans.end());
20    cout<<ans<<'\n';
21  }
22  return 0;
23}

Editorial not yet generated for this problem. Run the editorial generation script to add hints and detailed explanations.