We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2e35e8d commit 6d96016Copy full SHA for 6d96016
811_SubdomainVisitCount.swift
@@ -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
23
24
25
+ return subdomains
26
27
+}
0 commit comments