Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Shil and Palindrome problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Shil and Palindrome problem solution Shil is your new boss and he likes palindromes very much. A palindrome is a string that can be read the same way in either direction, from the left to the right and from the right to the left. (ex. madam, aabaa, racecar)
 
Given a string S, beautiful Palindrome is a lexicographical minimum palindrome that can be formed by rearranging all the characters of string S. In order to please your boss, find a beautiful Palindrome that can be formed with help of string S.
 
String x is lexicographically less than string y, if either x is a prefix of y (and x≠y), or there exists such i (1≤i≤min(|x|,|y|)), that xi<yi, and for any j (1≤j<i) xj=yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages.
 
 
HackerEarth Shil and Palindrome problem solution

 

 

HackerEarth Shil and Palindrome problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define INF 10000000
#define f first
#define pi pair<ll,ll>
#define pb emplace_back
#define mod 1000000007
#define s second
#define mp make_pair
#define fr freopen("input-15.txt","r",stdin)
#define fo freopen("output-15.txt","w",stdout)
int main(){
string s;
cin>>s;
ll n=s.length();
ll f[26]={0};
string ans=string(n,'0');
for(int i=0;i<s.length();i++) f[s[i]-'a']++;
if(n%2==0){
for(int i=0;i<26;i++){
if(f[i]%2!=0){
cout<<-1;
return 0;
}
}
}
else{
ll cnt=0;
char c;
for(int i=0;i<26;i++){
if(f[i]%2!=0) cnt++,c=char(i+'a');
}
if(cnt!=1){
cout<<-1;
return 0;
}
ans[n/2]=c;
f[c-'a']--;
}
ll j=0;
for(int i=0;i<26;i++){
while(f[i]>0){
f[i]-=2;
ans[j]=char(i+'a');
ans[n-j-1]=char(i+'a');
j++;
}
}
cout<<ans;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes