Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Unique sorting problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes