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
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Difficult Characters problem solution

YASH PAL, 31 July 202414 February 2026
In this HackerEarth Difficult Characters problem solution Yesterday, while Omar was trying to learn English, he saw that there are letters repeated many times in words while some other letters repeated only a few times or not repeated at all!
 
Of course, anyone can memorize the letters (repeated many times) better than the letters repeated few times, so Omar will concatenate all the words in the context he has, and try to know the difficulty of each letter according to the number of repetitions of each letter.
 
So Omar has now the whole context and wants to arrange the letters from the most difficult letter (repeated few times) to the less difficult letter (repeated many times).
 
If there are 2 letters with the same level of difficulty, the letter with a higher value of ASCII code will be more difficult.
 
 
HackerEarth Difficult Characters problem solution

 

 

HackerEarth Difficult Characters problem solution.

#include <bits/stdc++.h>

using namespace std;

int arr [26 + 1];
bool vis [26 + 1];

int main()
{
int n , t;
string s;

scanf("%d", &t);

while(t--){

memset(arr , 0 , sizeof arr);
memset(vis , false, sizeof vis);

cin >> s;

int len = s.size();

for(int i = 0; i < len; i++){

arr[ s[i] - 'a' ]++;
}

for(int i = 0; i < 26; i++){

int mini = 1e9;
char miniChar = '/';

for(int j = 25; j >= 0; j--){

if(arr[j] < mini && !vis[j]){

mini = arr[j];
miniChar = j + 'a';
}
}

vis[miniChar - 'a'] = true;

printf("%c ", miniChar);
}

printf("n");
}

return 0;
}
 

Second solution

#include <bits/stdc++.h>

using namespace std;

#define bug() printf("BUGn")
#define bug1(n) printf("bg1 %dn",n)
#define bug2(a,b) printf("bg2 %d %dn",a,b)
#define MOD 1000000007
#define ll long long
#define vi vector< int >
#define vll vector< long long >
#define vvi vector< vector< int > >
#define vvll vector< vector< long long > >
#define pi pair< int, int >
#define pll pair< long long, long long >
#define vpi vector< pair< int, int > >
#define vpll vector< pair< long long, long long > >
#define pb(n) push_back(n)
#define mp(a,b) make_pair(a,b)
#define printA(a,n) for(int i = 0;i < n;++i) cout<<a[i]<<" "; cout<<endl;

int main()
{
int t;
scanf("%d",&t);
assert(t >= 1 && t <= 10);
while(t--)
{
string s;
cin>>s;
vpi v;
for (int i = 0; i < 26; ++i)
{
v.pb(mp(0,i));
}
int len = s.length();
assert(len >= 1 && len <= 1000000);
for (int i = 0; i < len; ++i)
{
assert(s[i] >= 'a' && s[i] <= 'z');
v[s[i]-'a'].first += 1;
}
sort(v.begin(),v.end());
for (int i = 0,j; i < 26; ++i)
{
j = i+1;
int temp = i;
for (; j < 26; ++j,++i)
{
if(v[j].first != v[i].first)
break;
}
reverse(v.begin()+temp,v.begin()+j);
}
for (int i = 0; i < 26; ++i)
{
printf("%c ", v[i].second + 'a');
}
printf("n");
}
return 0;
}
 
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