Why my code is failing for large test cases


#1

void solve1(string &A,int &B,string &str,int size){

if(A.size()==size  || B<=0)
return;

if(A[size]==str[size])
solve1(A,B,str,size+1);
else {
    auto temp=max_element(A.begin()+size+1,A.end()) - A.begin();
    swap(A[size],A[temp]);
    B--;
    solve1(A,B,str,size+1);
}

}
bool compare(char a,char b){
return a>b;
}
string Solution::solve(string A, int B) {
string str=A;int size=0;
sort(str.begin(),str.end(),compare);
solve1(A,B,str,size);
return A;
}