@@ -4,23 +4,19 @@ def subdomainVisits(self, cpdomains):
44 :type cpdomains: List[str]
55 :rtype: List[str]
66 """
7- resList = []
8- resMap = {}
9- for s in cpdomains :
10- count , domains = s .split (' ' )
11- n = domains .count ('.' )
12- tmp = domains
13- for i in range (n + 1 ):
14- if resMap .has_key (tmp ):
15- resMap [tmp ] = resMap [tmp ] + int (count )
16- else :
17- resMap [tmp ] = int (count )
18- index = tmp .find ('.' ) + 1
19- if index == - 1 :
20- break
21- else :
22- tmp = tmp [index :]
23- # for key, value in resMap.items():
24- # resList.append(str(value) + ' ' + key);
25- # return resList
26- return [str (resMap [key ]) + ' ' + key for key in resMap ]
7+ from collections import defaultdict
8+ dic = defaultdict (int )
9+ 10+ for pair in cpdomains :
11+ splitted_pair = pair .split ()
12+ cnt , domain = splitted_pair [0 ], splitted_pair [1 ]
13+ cnt = int (cnt )
14+ 15+ for i in range (len (domain )):
16+ if not i or domain [i ] == "." :
17+ dic [domain [i :].lstrip ("." )] += cnt
18+ 19+ res = []
20+ for domain , frequency in dic .items ():
21+ res .append (" " .join ([str (frequency ), domain ]))
22+ return res
0 commit comments