Leetcode 829 Solution

This article provides solution to leetcode question 829 (subdomain-visit-count)

https://leetcode.com/problems/subdomain-visit-count

Solution

class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        m = collections.defaultdict(int)
        for cpdomain in cpdomains:
            cnt, domain = cpdomain.split(" ")
            cnt = int(cnt)

            tokens = domain.split(".")
            for i in range(len(tokens)):
                k = ".".join(tokens[i:])
                m[k] += cnt

        return ["{} {}".format(v, k) for k, v in m.items()]