[
string
hash table
]
Leetcode 0811 Subdomain Visit Count
Problem statement
https://leetcode.com/problems/subdomain-visit-count/
Solution
For each count-paired domain, first split it by space, then iterate over it, and if we meet ., add element to counter. Also add full url to counter as well.
Complexity
Time complexity is O(n), where n is total length of all cpdomains, because it is given, that we can have only 1 or 2 dots in address. Space complexity is also O(n).
Code
class Solution:
def subdomainVisits(self, cpdomains):
cnt = Counter()
for cpdomain in cpdomains:
count, url = cpdomain.split()
cnt[url] += int(count)
for i in range(len(url)):
if url[i] == ".": cnt[url[i+1:]] += int(count)
return [str(t) + " " + c for c, t in cnt.items()]