C++ | Maximal String | backtrack approach


#1
string generate (string &A, int B, int index){
    if(B == 0)              return A;
    string temp = A;
    for(int i=0; i<A.length(); i++) {
        if( i == index )    continue;
        swap(A[index], A[i]);
        temp = max(temp, generate(A, B-1, i));
    }
    return temp;
}
string Solution::solve(string A, int B) {
    string temp = A;
    for(int i=0; i<A.length(); i++)
        temp = max(temp, generate(A, B, i));
    return temp;
}