-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
28 lines (27 loc) · 809 Bytes
/
solution.js
File metadata and controls
28 lines (27 loc) · 809 Bytes
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
/**
* @param {string[]} cpdomains
* @return {string[]}
*/
var subdomainVisits = function(cpdomains) {
let visitedMap = {},
res = []
cpdomains.forEach(cpdomain => {
let baseCount = parseInt(cpdomain.split(' ')[0]),
domain = cpdomain.split(' ')[1],
domainSplits = domain.split('.'),
start = domainSplits.length - 1
while (start >= 0) {
let subDomain = domainSplits.slice(start).join('.')
if (visitedMap[subDomain] === undefined) {
visitedMap[subDomain] = baseCount
} else {
visitedMap[subDomain] += baseCount
}
start--
}
})
for (let key in visitedMap) {
res.push(`${visitedMap[key]} ${key}`)
}
return res
};