1399. Count Largest Group

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

On This Page

LeetCode problem 1399

class Solution:
    def countLargestGroup(self, n: int) -> int:
        cnt = Counter()
        res = mx = 0
        for i in range(1, n + 1):
            s = 0
            while i:
                s += i % 10
                i //= 10
            cnt[s] += 1
            if mx < cnt[s]:
                mx = cnt[s]
                res = 1
            elif mx == cnt[s]:
                res += 1
        return res