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 Unique sorting problem solution

YASH PAL, 31 July 202415 February 2026
In this HackerEarth Unique sorting problem solution, You are given a string s consisting of numeric digits (0-9) and Latin alphabetic characters (‘a’-‘z’) only.
 
Your task is to sort the string but it must be sorted in a unique manner because here even characters or digits have higher priority than odd characters or digits. Although, as usual, other characters or digits have the same priority level. Also, digits have higher priority than characters.
 
 
HackerEarth Unique sorting problem solution

 

 

HackerEarth Unique sorting problem solution.

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

void solve(){
string s;
cin>>s;
int cf[26]{0},df[10]{0};
for(char c:s){
if(isdigit(c)) df[c-'0']++;
else cf[c-'a']++;
}
string ans;
for(int i=1;i<26;i+=2){
rep(j,1,cf[i]) ans+=char('a'+i);
}
for(int i=0;i<26;i+=2){
rep(j,1,cf[i]) ans+=char('a'+i);
}
for(int i=1;i<10;i+=2){
rep(j,1,df[i]) ans+=char('0'+i);
}
for(int i=0;i<10;i+=2){
rep(j,1,df[i]) ans+=char('0'+i);
}
cout<<ans<<'n';
}
signed main(){
FastIO;
int t;
cin>>t;
while(t--) solve();
return 0;
}
 

Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 14;
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int t = 1;
cin >> t;
while(t--){
string s;
cin >> s;
sort(s.begin(), s.end(), [](char c, char d) -> bool{
if(isalpha(c) != isalpha(d))
return isalpha(c);
if(isalpha(c) ? (c - 'a') % 2 != (d - 'a') % 2 : (c - '0') % 2 != (d - '0') % 2)
return isalpha(c) ? (c - 'a') % 2 > (d - 'a') % 2 : (c - '0') % 2 > (d - '0') % 2;
return c < d;
});
cout << s << 'n';
}
}
 
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