Fast C++ Solution without using STL function


#1

string Solution::solve(string nums) {

int n=nums.size(), i, j;
    for(i=n-2; i>=0; i--)
        if(nums[i]<nums[i+1]) break;

    if(i==-1) return "-1";
    else 
        for(j=n-1; j>i; j--){
            if(nums[j]>nums[i]) break;
                swap(nums[i],nums[j]);

}
reverse(nums.begin()+i+1,nums.end());
return nums;
}