All Problems

Problem title

Difficulty

Avg time to solve

Number of rectangles in a grid

Easy

15 mins

Print Parentheses

Easy

10 mins

Find nth elements of spiral matrix

Easy

15 mins

Combination Sum

Easy

15 mins

The N - Queens Puzzle

Moderate

--

LCA In A BST

Moderate

15 mins

Ninja Fight

Easy

10 mins

Next Greater Number

Moderate

15 mins

Covid Spread

Hard

45 mins

Binary Numbers

Easy

10 mins

Problem

Submissions

8

Avg. time to solve

15 min

Success Rate

90%

Problem Statement

```
If the given string is 56789, then the next greater number is 56798. Note that although 56790 is also greater than the given number it contains 1 '0' which is not in the original number and also it does not contain the digit '8'.
```

```
The given string is non-empty.
If the answer does not exist, then return -1.
The given number does not contain any leading zeros.
```

```
The first line of the input contains an integer T denoting the number of test cases.
The first and the only line of each test case contains a string S representing the number.
```

```
The only line of output of each test case should print the number which is just greater than the given number as described above
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 100
1 <= len(S) <= 10^4
Time Limit: 1 sec
```

```
1
1234
```

```
1243
```

```
1243 is the next greater number consisting of the same set of digits (1,2,3 and 4)
```

```
2
4321
65312
```

```
-1
65321
```

Java (SE 1.8)

Console

Sample Test Case

Custom Test Case

Download Test Cases

Test Case 1

Test Case 2

Test Case 3

Saving Code...

Full Screen Mode

Change Theme

Solution submission not allowed

Save Code

Reset Code