<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:
        
        def find_combinations(n):
            combinations = []

            for i in range(1, ceil(n / 2)):
                current_combo = [i, n - i]
                combinations.append(current_combo)

                deeper_combos = find_combinations(n - i)
                for combo in deeper_combos:
                    repeat = False
                    for el in combo:
                        if repeat:
                            break
                        if el in current_combo:
                            repeat = True
                    if not repeat:
                        new_combo = [i]
                        for el in combo:
                            new_combo.append(el)
                        new_combo = sorted(new_combo)
                        if new_combo not in combinations:
                            combinations.append(new_combo)

            return combinations
        
        def permute(s):
            result = []
            if len(s) == 1:
                return [s]
            for i, char in enumerate(s):
                for perm in permute(s[:i] + s[i + 1:]):
                    result.append(char + perm)
            return result
        
        
        
        
        length_next_number = len(str(n))

        discharge_limit = int(str(length_next_number) * length_next_number)
        if n >= discharge_limit:
            length_next_number += 1

        combos = find_combinations(length_next_number)
        possible_bases = []
        all_numbers_discharge = []
        all_numbers_discharge.append(int(str(length_next_number) * length_next_number))

        for combo in combos:
            current_base = ''
            
            for el in combo:
                current_base = current_base + (str(el) * el)

            possible_bases.append(current_base)

        for base in possible_bases:
            all_perms = permute(base)
            all_perms = list(set(all_perms))
            all_numbers_discharge += [int(el) for el in all_perms]

        all_numbers_discharge = sorted(all_numbers_discharge)

        for next_balanced in all_numbers_discharge:
            if next_balanced > n:
                return int(next_balanced)

        return 0
246