In short my approach is:


#1
string Solution::solve(string A) {
    int n=A.length();
    int i=n-1;
    int j=n-1;
    while(i>=1)
    {
        if(A[i-1]<A[i])
        break;
        i--;
    }
    if(i==0)
    return "-1";
    while(j>=i && A[i-1]>=A[j])
    j--;
    swap(A[i-1],A[j]);
    reverse(A.begin()+i,A.end());
    return A;
}

Dry run this code if you find any difficulty…