<p>An integer <code>x</code> is <strong>numerically balanced</strong> if for every digit <code>d</code> in the number <code>x</code>, there are <strong>exactly</strong> <code>d</code> occurrences of that digit in <code>x</code>.</p>

<p>Given an integer <code>n</code>, return <em>the <strong>smallest numerically balanced</strong> number <strong>strictly greater</strong> than </em><code>n</code><em>.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 22
<strong>Explanation:</strong> 
22 is numerically balanced since:
- The digit 2 occurs 2 times. 
It is also the smallest numerically balanced number strictly greater than 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1000
<strong>Output:</strong> 1333
<strong>Explanation:</strong> 
1333 is numerically balanced since:
- The digit 1 occurs 1 time.
- The digit 3 occurs 3 times. 
It is also the smallest numerically balanced number strictly greater than 1000.
Note that 1022 cannot be the answer because 0 appeared more than 0 times.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 3000
<strong>Output:</strong> 3133
<strong>Explanation:</strong> 
3133 is numerically balanced since:
- The digit 1 occurs 1 time.
- The digit 3 occurs 3 times.
It is also the smallest numerically balanced number strictly greater than 3000.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= n &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution:
    def nextBeautifulNumber(self, n: int) -> int:
        balanced_numbers = {1224444,666666, 555551, 555515,555155,551555,515555,155555,224444,242444,244244,244424,244442,422444,424244,424424,424442,442244,442424,442442,444224,444242,444422,122333,123233,123323,123332,132233,132323,132332,133223,133232,133322,212333,213233,213323,213332,312233,312323,312332,313223,313232,313322,221333,231233,231323,231332,321233,321323,321332,331223,331232,331322,223133,232133,233123,233132,322133,323123,323132,332123,332132,333122,223313,232313,233213,233312,322313,323213,323312,332213,332312,333212,223331,232331,233231,233321,322331,323231,323321,332231,332321,333221,55555,44441,44414,44144,41444,14444,22333,23233,23323,23332,32233,32323,32332,33223,33232,33322,4444,3331,3313,3133,1333,333,221,212,122,22,1}
        ans = 1224444
        for i in balanced_numbers:
            if i > n:
                ans = min(i,ans)
        return ans
        

             
59