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
Inputn = 1
Output22
22 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
Inputn = 1000
Output1333
1333 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

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate