<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>

# 1
# 22

# 1 --> [000] 1000
# 1 --> [333]

# 3 --> [000]
# 3 --> [133]

# 2%10
# 21



class Solution:
    # def nextBeautifulNumber(self, n: int) -> int:
    #     def isBeautifulNumber(n):
    #         freq: int = [0] * 10

    #         while n > 0:
    #             rigth_most: int = n%10
    #             if rigth_most == 0: 
    #                 return False
    #             freq[rigth_most] += 1
    #             n //= 10

    #         for idx in range(1,10):
    #             if freq[idx] != 0 and freq[idx] != idx:
    #                 return False

    #         return True

    #     while True:
    #         n = n+1
    #         if isBeautifulNumber(n):
    #             return n
    
    def nextBeautifulNumber(self, n: int) -> int:
        def is_beautiful(num: int) -> bool:
            digit_count = [0] * 10

            for digit in str(num):
                if int(digit) == 0:
                    return False
                
                digit_count[int(digit)] += 1
            
            # Check if every digit count matches the digit itself (ignoring zeros)
            for d in range(1, 10):
                if digit_count[d] != 0 and digit_count[d] != d:
                    return False
                
            return True

        # Start checking from n + 1 upwards
        while True:
            n += 1
            if is_beautiful(n):
                return n

2742