Leetcode 829 Solution
This article provides solution to leetcode question 829 (subdomain-visit-count)
Access this page by simply typing in "lcs 829" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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()]