Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth The smallest string problem solution

YASH PAL, 31 July 2024
In this HackerEarth The smallest string problem solution You are given a string S which consists of lower case Latin letters and you need to perform the following operation exactly K times:
Select any character and replace it with its next character [‘a’ with ‘b’, ‘b’ with ‘c’…. ‘z’ with ‘a’].
You need to find the lexicographically smallest string after performing exactly K operations on string S.
HackerEarth The smallest string problem solution

HackerEarth The smallest string problem solution.

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
#define endl "n"
#define test ll txtc; cin>>txtc; while(txtc--)
typedef long long int ll;
typedef long double ld;
int main() {
FIO;
test
{
ll n,k,ops; cin>>n>>k;
string s; cin>>s;
for(ll i=0;i+1<n;i++){
ops=('z'-s[i]+1)%26;
if(k>=ops){
s[i]='a';
k-=ops;
}
}
k%=26;
ops=(s[n-1]-'a');
ops+=k;
ops%=26;
s[n-1]=char('a'+ops);
cout<<s<<endl;
}
return 0;
}

Second solution

t = int(input())
while t > 0:
t -= 1
n, k = map(int, input().split())
s = list(input())
for i in range(n):
if (ord('z') - ord(s[i]) + 1) % 26 <= k:
k -= (ord('z') - ord(s[i]) + 1) % 26
s[i] = 'a'
k %= 26
s[-1] = chr(ord(s[-1]) + k if ord(s[-1]) + k <= ord('z') else ord(s[-1]) + k - 26)
print(''.join(s))
coding problems solutions

Post navigation

Previous post
Next post

Related website

The Computer Science

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes