Skip to content

Commit a131ec7

Browse files
authored
Create subdomain_visit_count.py
1 parent 97b6886 commit a131ec7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

subdomain_visit_count.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
"""
3+
@param cpdomains: a list cpdomains of count-paired domains
4+
@return: a list of count-paired domains
5+
"""
6+
def subdomainVisits(self, cpdomains):
7+
# Write your code here
8+
di = {}
9+
for d in cpdomains:
10+
count, domain = d.split(' ')
11+
count = int(count)
12+
parts = domain.split('.')
13+
for i in range(1, len(parts) + 1): # 拆分组合
14+
sd = '.'.join(parts[len(parts) -i:])
15+
if sd not in di:
16+
di[sd] = count
17+
else:
18+
di[sd] += count
19+
ret = []
20+
for k, v in di.items():
21+
ret.append(str(v) + ' ' + k)
22+
return ret
23+
24+
# easy: https://www.lintcode.com/problem/subdomain-visit-count

0 commit comments

Comments
 (0)