**Problem Description**

Given a number **A** in a form of string.

You have to find the smallest number that has same set of digits as A and is greater than A.

If A is the greatest possible number with its set of digits, then return -1.

1 <= A <= 10^{100000}

A doesn't contain leading zeroes.

First and only argument is an numeric string denoting the number **A**.

Return a string denoting the smallest number greater than A with same set of digits , if A is the largest possible then return -1.

Input 1:

A = "218765"

Input 2:

A = "4321"

Output 1:

"251678"

Output 2:

"-1"

Explanation 1:

The smallest number greater then 218765 with same set of digits is 251678.

Explanation 2:

The given number is the largest possible number with given set of digits so we will return -1.

NOTE:You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.

Sign Up

to access hints and editorial solutions for**Next Similar Number**

to access hints and editorial solutions for

Loading...