-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.go
More file actions
36 lines (30 loc) · 764 Bytes
/
solution.go
File metadata and controls
36 lines (30 loc) · 764 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
29
30
31
32
33
34
35
36
package subdomain_visit_count
import (
"fmt"
"strconv"
"strings"
)
func subdomainVisits(cpdomains []string) []string {
resultMap := map[string]int{}
for _, cpdomain := range cpdomains {
domainSplit := strings.Split(cpdomain, " ")
count, _ := strconv.Atoi(domainSplit[0])
domain := domainSplit[1]
tmpDomain := domain
for tmpDomain != "" {
resultMap[tmpDomain] += count
tmpDomainSplit := strings.Split(tmpDomain, ".")
if len(tmpDomainSplit) > 0 {
tmpDomainSplit = tmpDomainSplit[1:]
} else {
tmpDomainSplit = []string{}
}
tmpDomain = strings.Join(tmpDomainSplit, ".")
}
}
var result []string
for domain, count := range resultMap {
result = append(result, fmt.Sprintf("%d %s", count, domain))
}
return result
}