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.

&nbsp;
Example 1:

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


Example 2:

Input: n = 1000
Output: 1333
Explanation: 
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.


Example 3:

Input: n = 3000
Output: 3133
Explanation: 
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.


&nbsp;
Constraints:


	0 &lt;= n &lt;= 106


class Solution:
    def nextBeautifulNumber(self, n: int) -> int:
        n_digits = len(str(n))
        
        next_max = {
            1: [1],
            2: [22],
            3: [122, 333],
            4: [1333, 4444],
            5: [14444, 22333, 55555],
            6: [122333, 224444, 666666, 155555],
            7: [1224444, 2255555, 3334444, 1666666, 7777777]
                }
        
        if n >= int(str(n_digits) * n_digits):
            n_digits += 1
            return min(next_max[n_digits])
        
        ans = float('inf')
        for num in sorted(next_max[n_digits]):      
            cands = set(permutations(str(num)))
            cands = sorted(map(lambda x: int("".join(x)), cands))
        
            loc = bisect.bisect(cands, n)
            if loc < len(cands):           
                ans = min(ans, cands[loc])
        
        return ans
class Solution {
    public int nextBeautifulNumber(int n) {

        while(true){
            n++;
           int num = n; //test this number
           int [] freq = new int[10]; // 0 to 9

            while(num > 0){ //calculate freq of each digit in the num
                int rem = num % 10; //this is remainder
                num = num / 10; //this is quotient
                freq[rem] = freq[rem] + 1; //increase its frequency
                if(freq[rem] > rem) break;
            }

            boolean ans = true;

            for(int i = 0;i<10;i++){ //check frequency of each digit
              if(freq[i] != i && freq[i] != 0){
                  ans = false;
                  break;
              }
            }

            if(ans == true){
                return n;
            }
        }
    }
}
class Solution {
public:
    bool valid(int n)
    {
        vector<int> map(10,0);
        while(n)
        {
            int rem = n%10;
            map[rem]++;
            n = n/10;
        }
        for(int i=0; i<10; i++)
            if(map[i] && map[i]!=i) return false;
        return true;
    }
    
    int nextBeautifulNumber(int n) {
     
        while(true) 
        {
            ++n;
            if(valid(n))
                return n;
        }
        return 1;
    }
};
/**
 * @param {number} n
 * @return {number}
 */
var nextBeautifulNumber = function(n) {
    //1224444 is next minimum balanced number after 10^6
    for(let i=n+1; i<=1224444;i++){//Sequency check each number from n+1 to 1224444
        if(isNumericallyBalanced(i)){
            return i;//Return the number if is a balanced number
        }
    }
    function isNumericallyBalanced(n){
        let map={},d,nStr=n.toString();
        while(n>0){//Create a map of digits with frequency
            d = n%10;
            if(d>nStr.length){//If we have found a digit greater than the lenght of the number like 227333, here when we see 7, we can return false
                return false;
            }
            if(map[d]===undefined){
                map[d]=1;
            }else{
                map[d]++;
                if(map[d]>d){//If a digit has frequency more than its value, for example 22333344, here when we see fourth 3 than we can return false
                    return false;
                }
            }
            n = Math.floor(n/10);
        }
        for(let key in map){//Check if frequency is equal to the digit
            if(map[key]!==parseInt(key)){
                return false;
            }
        }
        return true;
    }
};
Next Greater Numerically Balanced Number