Java Editorial Fastest solution has a bug


#1

Though Java Editorial Fastest solution passes the test cases present by the question setter, it fails for following test case.

A=9867 B=1, Here, Java editorial fastest solution returns 9867 but correct answer after all possible swaps should be 9876

This question is also lacking test cases from problem setter end.

Correct code for java editorial fastest solution should be:

public class Solution {
String max ;

public void cal(char Str[],int K,int cur)
{
    if(K == 0)
    {
        return;
    }
    
    char maxC = Str[cur];
    int N = Str.length;
    
    for(int j=cur+1;j<N;j++)
    {
        if(maxC  < Str[j])
        {
            maxC = Str[j];
        }
    }
    
    if(maxC != Str[cur])
    {
        K--;
    }
    
    for(int j=cur+1;j<N;j++)
    {
        if(maxC == Str[j])
        {
            char temp  =Str[j];
            Str[j] = Str[cur];
            Str[cur] = temp;
            
            String stp = new String(Str);
            
            if(max.compareTo(stp) < 0)
            {
                max = stp;
            }
            
            cal(Str,K,cur+1);
            
            temp  =Str[j];
            Str[j] = Str[cur];
            Str[cur] = temp;
        }
    }
    
    if(Str[cur]==maxC) cal(Str,K,cur+1);    // This line missed
    
}

public String solve(String A, int B) {
    max = A;
    cal(A.toCharArray(),B,0);
    return max;
}
}