LeetCode solutions

2174. Next Greater Numerically Balanced Number

My accepted Python solution to LeetCode problem 2174, Next Greater Numerically Balanced Number, running in 4523ms.

  • Difficulty: Medium
  • Python
  • Runtime 4523ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4523ms, memory 17.4MB, accepted 2025-12-29.

python
class Solution:
    def nextBeautifulNumber(self, n: int) -> int:
        def isBalanced(num):
            s = str(num)
            from collections import Counter
            cnt = Counter(s)
            for digit, count in cnt.items():
                if int(digit) != count:
                    return False
            return True
        
        num = n + 1
        while not isBalanced(num):
            num += 1
        return num

Source