-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSubdomain Visit Count Pt2.py
38 lines (33 loc) · 1.21 KB
/
Subdomain Visit Count Pt2.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
# Subdomain Visit Count
# https://leetcode.com/problems/subdomain-visit-count/description/
class Solution(object):
def __init__(self):
# key = domain name, value = count
self.domainHash = {}
def getDomains(self, cpdomains):
for domains in cpdomains:
array = domains.split(" ")
visitCount = array[0]
domainNames = array[1].split(".")
# get all the subdomains
subDomain = ""
for d in reversed(domainNames):
subDomain = d +"."+ subDomain
subDomainName = subDomain[:-1]
if (subDomainName not in self.domainHash):
self.domainHash[subDomainName] = 0
# increment the count
self.domainHash[subDomainName] += int(visitCount)
def getList(self):
# create a list from the hash
output = []
for key in self.domainHash:
output.append(str(self.domainHash[key]) +" "+ key)
return output
def subdomainVisits(self, cpdomains):
self.getDomains(cpdomains)
return self.getList()
"""
:type cpdomains: List[str]
:rtype: List[str]
"""