Very Simple C++ solution using iterative method


#1

string Solution::solve(string A, int B)
{
string s=A;
int i=0;
int index=-1;
while(B–)
{
int maxi=0;
for(int j=i;j<A.length();j++)
{
if(A[j]-‘0’>maxi)
{
index=j;
maxi=A[j]-‘0’;
}
}
swap(A[index],A[i]);
if(A[i]==s[i])
{
B++;
}
i++;
}
return A;

}


#2

your answer is wrong , its just that testcases are weak here , try A=5799 , B=2 , with your code , your code will output 9957 , where as the answer 9975 (you can verify that by expected output , and if you would have been brain enough you would have understood yourself ).