Header Ad

HackerEarth Greatest String problem solution

In this HackerEarth Greatest String problem solution, You are given a string S and an integer Q. You are allowed to perform at most Q operations on the string. In one operation, you can change any vowel to it's next character (e.g., 'a'->'b', 'e'->'f', 'i'->'j', 'o'->'p', 'u'->'v'). Generate the lexicographically greatest string by performing at most Q operations on string S.


HackerEarth Greatest String problem solution


HackerEarth Greatest String problem solution.

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define si(x) scanf("%d",&x)
#define pi(x) printf("%d\n",x)
#define s(x) scanf("%lld",&x)
#define p(x) printf("%lld\n",x)
#define sc(x) scanf("%s",x)
#define pc(x) printf("%s",x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define inf 1e18
#define prec(x) fixed<<setprecision(15)<<x
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define mem(x,y) memset(x,y,sizeof(x))
#define PQG priority_queue< int,std::vector<int>,std::greater<int> >
#define PQL priority_queue< int,std::vector<int>,std::less<int> >
#define PQPL priority_queue<pii ,vector< pii >, less< pii > >
#define PQPG priority_queue<pii ,vector< pii >, greater< pii > >
#define PQPGB priority_queue<pii ,vector< pll >, greater< pll > >
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)

using namespace std;

bool isvowel(char c) {
return (c=='a' || c=='e' || c=='i' || c=='o' || c=='u');
}

int main() {
#ifndef ONLINE_JUDGE
freopen("inp.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
int t; cin>>t;
assert(t>=1 && t<=10);
while(t--) {
string s; cin>>s;
int q; cin>>q;
int n=s.size();
assert(n>=1 && n<=100000);
assert(q>=0 && q<=100000);
for(int i=0;i<n;i++) {
if(q>0) {
if(isvowel(s[i])) {
int x=(s[i]-'a')+1;
s[i]=char(x+'a');
q--;
}
}
}
cout<<s<<endl;
}


return 0;
}


Second solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
assert(cin>>t);
assert(t>=1 && t<=1e1);
while(t--)
{
string s;
assert(cin>>s);
assert(s.size()>=1 && s.size()<=1e5);
int q;
assert(cin>>q);
assert(q>=0 && q<=1e5);
int cnt=0;
for(int i=0;i<s.size();i++)
{
assert(s[i]>='a' && s[i]<='z');
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u')
{
if(cnt<q)
{
s[i]=(char)(s[i]+1);
cnt++;
}
}
}
cout<<s<<"\n";
}
return 0;
}


Post a Comment

0 Comments