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 Kth Character problem solution

YASH PAL, 31 July 2024
In this HackerEarth Kth Character problem solution, You are given a string S of lowercase alphabets. Now you need to remove all the instances of exactly one type of character such that the new string that is formed is lexicographically smallest across all the other strings that can be formed in a similar way.
For example: Let the string S be “appleap”
If you remove all instances of ‘a’ then the string formed will be : pplep
If you remove all isntances of ‘p’ then the string formed will be : alea
If you remove all intsances of ‘l’ then the string formed will be : appeap
If you remove all instances of ‘e’ then the string formed will be : applap
So among all the newly formed strings , alea is lexicographically smallest string. 
HackerEarth Kth Character problem solution

HackerEarth Kth Character problem solution.

#include<bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define endl "n"
#define eps 0.00000001
LL pow(LL a,LL b,LL m){LL x=1,y=a;while(b > 0){if(b%2 == 1){x=(x*y);if(x>m) x%=m;}y = (y*y);if(y>m) y%=m;b /= 2;}return x%m;}
LL gcd(LL a,LL b){if(b==0) return a; else return gcd(b,a%b);}
LL gen(LL start,LL end){LL diff = end-start;LL temp = rand()%start;return temp+diff;}
using namespace std;
vector<string> v;
int main()
{
ios_base::sync_with_stdio(0);
string s;
cin >> s;
int mask = 0;
for(int i = 0; i < s.length(); i++)
{
mask = (mask | (1 << (s[i] - 'a')));
}
for(int i = 0; i < 26; i++)
{
if(mask & (1 << i))
{
string temp;
for(int j = 0; j < s.length(); j++)
{
if((s[j] - 'a') != i)
{
temp.push_back(s[j]);
}
}
v.push_back(temp);
}
}
sort(v.begin() , v.end());
cout << v[0];
}

Second solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int n=s.size();
vector<string>v;
for(char i='a';i<='z';i++)
{
string temp="";
for(int j=0;j<n;j++)
{
if(s[j]!=i)
temp+=s[j];
}
v.push_back(temp);
}
sort(v.begin(),v.end());
cout<<v[0]<<"n";
return 0;
}
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