Skip to content

Latest commit

 

History

History
27 lines (25 loc) · 734 Bytes

811.md

File metadata and controls

27 lines (25 loc) · 734 Bytes

811. Subdomain Visit Count

Solution 1 (time O(n), space O(n))

class Solution(object):
    def subdomainVisits(self, cpdomains):
        """
        :type cpdomains: List[str]
        :rtype: List[str]
        """
        d = collections.defaultdict(int)
        for c in cpdomains:
            freq, domain = c.split()
            t = domain.split(".")
            cur = ""
            for i in range(len(t) - 1, -1, -1):
                if cur:
                    cur = t[i] + "." + cur
                else:
                    cur = t[i]
                d[cur] += int(freq)
        ans = []
        for k, v in d.items():
            ans.append(str(v) + " " + k)
        return ans