forked from fredfeng0326/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
let811.py
43 lines (36 loc) · 1.31 KB
/
let811.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 811. Subdomain Visit Count
import collections
class Solution:
def subdomainVisits(self, cpdomains):
"""
:type cpdomains: List[str]
:rtype: List[str]
"""
dic = {}
for item in cpdomains:
number = int(item.split(' ')[0])
domains = item.split(' ')[1].split('.')
times = len(domains)
for time in range(times):
key = '.'.join(domains[time:])
n = number
if key in dic:
dic[key] += n
else:
dic[key] = n
out = []
for key, val in dic.items():
out.append(str(val) + ' ' + str(key))
return out
def subdomainVisits2(self, cpdomains):
ans = collections.Counter()
for domain in cpdomains:
count, domain = domain.split()
count = int(count)
frags = domain.split('.')
for i in range(len(frags)):
ans[".".join(frags[i:])] += count
return ["{} {}".format(ct, dom) for dom, ct in ans.items()]
a = Solution()
print(a.subdomainVisits(["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]))
print(a.subdomainVisits2(["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]))