2048. Next Greater Numerically Balanced Number

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2048

class Solution:
    def nextBeautifulNumber(self, n: int) -> int:
        for x in count(n + 1):
            y = x
            cnt = [0] * 10
            while y:
                y, v = divmod(y, 10)
                cnt[v] += 1
            if all(v == 0 or i == v for i, v in enumerate(cnt)):
                return x