-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution811.go
More file actions
34 lines (28 loc) · 848 Bytes
/
solution811.go
File metadata and controls
34 lines (28 loc) · 848 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
package solution811
import (
"strconv"
"strings"
)
// ============================================================================
// 811. Subdomain Visit Count
// URL: https://leetcode.com/problems/subdomain-visit-count/description/
// ============================================================================
func subdomainVisits(cpdomains []string) []string {
visits := make(map[string]int)
for _, info := range cpdomains {
parts := strings.Fields(info)
count, _ := strconv.Atoi(parts[0])
domain := parts[1]
subs := strings.Split(domain, ".")
for i := 0; i < len(subs); i++ {
subdomain := strings.Join(subs[i:], ".")
visits[subdomain] += count
}
}
var subdomains []string
for domain, count := range visits {
s := strconv.Itoa(count) + " " + domain
subdomains = append(subdomains, s)
}
return subdomains
}