Next Greater Numerically Balanced Number
An integer x is numerically balanced if for every digit d in the number x, there are exactly d occurrences of that digit in x.
Given an integer n, return the smallest numerically balanced number strictly greater than n.
Example 1
Input
n = 1Output
2222 is numerically balanced because the digit 2 occurs exactly 2 times, and it is the smallest numerically balanced number strictly greater than 1.
Example 2
Input
n = 1000Output
13331333 is numerically balanced because the digit 1 occurs exactly 1 time and the digit 3 occurs exactly 3 times, and 1022 is invalid because 0 appears more than 0 times.
Constraints
- 0 <= n <= 10^6