Skip to content

Commit 6d96016

Browse files
committed
Add solution 811
1 parent 2e35e8d commit 6d96016

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Diff for: 811_SubdomainVisitCount.swift

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
func subdomainVisits(_ cpdomains: [String]) -> [String] {
3+
var dict = [String: Int]()
4+
for item in cpdomains {
5+
let pair = item.split(separator: " ")
6+
let (count, domain) = (Int(pair[0])!, String(pair[1]))
7+
let subdomains = getSubdomains(of: domain)
8+
for sub in subdomains {
9+
dict[sub] = (dict[sub] ?? 0) + count
10+
}
11+
}
12+
return dict.map{item in "\(item.value) \(item.key)"
13+
}
14+
}
15+
16+
func getSubdomains(of domain: String) -> [String] {
17+
var subdomains = [String]()
18+
var subdomain = domain
19+
subdomains.append(subdomain)
20+
while let i = subdomain.index(of: ".") {
21+
subdomain = String(subdomain[subdomain.index(after:i)...])
22+
subdomains.append(subdomain)
23+
24+
}
25+
return subdomains
26+
}
27+
}

0 commit comments

Comments
 (0)