Convert each number from to string, and get its sum. Then keep track of how many times that sum repeats.

Using a counter, count the one with the largest value and return it.

Code

Python3

class Solution:
    def countLargestGroup(self, n: int) -> int:
 
        check = defaultdict(int)
 
        res = 0
        for i in range(1, n+1):
            S = sum(map(int, list(str(i))))
 
            if S in check:
                check[S] += 1
            else:
                check[S] = 1
 
        return Counter(check.values())[max(check.values())]

Big O Analysis

  • Runtime

    The runtime complexity here is since we are operating on every number from .

  • Memory

    The memory usage is since we use the collections.defaultdict object to store the repeat occurences.

— A

GitHub | Twitter