Talent Battle 100 Days Coding Series
Gru has a string S of length N, consisting of only characters a and b for banana and P points to spend.
Now Gru wants to replace and/or re-arrange characters of this given string to get the lexicographically
smallest string possible. For this, he can perform the following two operations any number of times.
1. Swap any two characters in the string. This operation costs 11point. (any two, need not be
adjacent)
2. Replace a character in the string with any other lower case english letter. This operation costs 2
points.
Help Gru in obtaining the lexicographically smallest string possible, by using at most P points.
Input:
First line will contain T, number of testcases. Then the testcases follow.
Each testcase contains two lines of input, first-line containing two integers N , P.
The second line contains a string S consisting of N characters.
Output: For each testcase, output in a single containing the lexicographically smallest string obtained.
Sample Input
33
bba
Sample Output
aab
C++
#include <bits/stdc++.h>
#include <fstream>
#include <cmath>
typedef long long int ll;
using namespace std;
Talent Battle 100 Days Coding Series
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;cin>>t;
while(t--){
int n,p;cin>>n>>p;
bool check[n];
memset(check,false,sizeof(check));
string s;cin>>s;
int arr[n];
memset(arr,0,sizeof(arr));
int count1=0; int count2=0;
for(int i=0;i<n;i++){
arr[i] = int(s[i]);
if(s[i]=='a'){
count1++;
}
int count=0;
for(int i=0;i<count1;i++){
if(arr[i]==int('b')){
count++;
int i=0;int k = p;int w = count;
while(i<count1 && k>0 && w>0){
if(arr[i]==int('b')){
arr[i] = int('a');
k--;w--;
Talent Battle 100 Days Coding Series
i++;
i = n-1;k = p;w = count;
while(i>=count1 && k>0 && w>0){
if(arr[i]==int('a')){
arr[i] = int('b');
check[i] = true;
k--;w--;
i--;
if(k>0){
int i = count1;
while(i<n && k>0){
if(check[i]==false){
if(k>1)
{
k-=2;
arr[i] = int('a');
else{
k--;
arr[i] = int('a');
i++;
}
Talent Battle 100 Days Coding Series
for(int i=0;i<n;i++){
cout<<char(arr[i]);
cout<<endl;
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t1=Integer.parseInt(br.readLine());
for(int t=1;t<=t1;t++)
StringTokenizer tk=new StringTokenizer(br.readLine());
int n=Integer.parseInt(tk.nextToken());
int p=Integer.parseInt(tk.nextToken());
String s=br.readLine();
char c[]=s.toCharArray();
int b=0;
for(int i=0;i<c.length;i++)
Talent Battle 100 Days Coding Series
if(s.charAt(i)=='b')
b++;
int a1=0;
for(int i=c.length-1;i>=n-b;i--)
if(c[i]=='a')
a1++;
if(p<=a1)
int p1=p;
for(int i=0;i<c.length&&p1>0;i++)
if(c[i]=='a')
continue;
else
{
c[i]='a';p1--;
p1=p;
for(int i=n-1;i>=0&&p1>0;i--)
if(c[i]=='b')
continue;
else
{
Talent Battle 100 Days Coding Series
c[i]='b';
p1--;
System.out.println(String.valueOf(c));
else
char c2[]=s.toCharArray();
Arrays.sort(c2);
int p1=p-a1;
for(int i=n-b;i<c.length&&p1>0;i++)
if(c[i]=='b')
if(p1>=2)
c2[i]='a';p1-=2;
}
else
c2[i]='a';p1--;
System.out.println(String.valueOf(c2));
}
Talent Battle 100 Days Coding Series
Python
for _ in range(int(input())):
n, p = map(int,input().split())
s = list(input())
a=s.count('a'); b=s.count('b'); swap = 0
arr = [i for i in s]
for i in range(a):
if s[i] == 'b':
swap += 1
if p <= swap:
i=0; tmp=p
while p>0 and i<n:
if arr[i]=='b':
arr[i]='a'
p -= 1
i += 1
p = tmp; i = n-1
while p>0 and i>0:
if arr[i] == 'a':
arr[i] = 'b'
p -= 1
i -= 1
print(''.join(arr))
else:
for j in range(n):
Talent Battle 100 Days Coding Series
if j < a:
arr[j] = 'a'
else:
arr[j] = 'b'
p -= swap
for k in range(n):
if arr[k] == 'b':
if s[k] == 'b' and p >= 2:
p -= 2
arr[k] = 'a'
if s[k] == 'a' and p >= 1:
p -= 1
arr[k] = 'a'
print(''.join(arr))