Skip to content

Commit 1c22d53

Browse files
committed
feat: add solutions to lc problem: No.0811
No.0811.Subdomain Visit Count
1 parent 5078ba8 commit 1c22d53

File tree

6 files changed

+204
-63
lines changed

6 files changed

+204
-63
lines changed

solution/0800-0899/0811.Subdomain Visit Count/README.md

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:哈希表**
55+
56+
我们用哈希表 `cnt` 存储每个域名(子域名)对应的访问次数。
57+
58+
然后遍历数组,对于每个域名,我们将其拆分为子域名,然后更新哈希表 `cnt` 中对应的访问次数。
59+
60+
最后,我们将哈希表中的键值对转换为数组,即可得到答案。
61+
62+
时间复杂度 $O(L)$,空间复杂度 $O(L)$。其中 $L$ 是数组 `cpdomains` 中所有域名的长度之和。
63+
5464
<!-- tabs:start -->
5565

5666
### **Python3**
@@ -60,15 +70,13 @@
6070
```python
6171
class Solution:
6272
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
63-
domains = Counter()
64-
for item in cpdomains:
65-
count, domain = item.split()
66-
count = int(count)
67-
subs = domain.split('.')
68-
for i in range(len(subs)):
69-
key = '.'.join(subs[i:])
70-
domains[key] += count
71-
return [f'{cnt} {domain}' for domain, cnt in domains.items()]
73+
cnt = Counter()
74+
for s in cpdomains:
75+
v = int(s[:s.index(' ')])
76+
for i, c in enumerate(s):
77+
if c in (' ', '.'):
78+
cnt[s[i + 1:]] += v
79+
return [f'{v} {s}' for s, v in cnt.items()]
7280
```
7381

7482
### **Java**
@@ -78,21 +86,70 @@ class Solution:
7886
```java
7987
class Solution {
8088
public List<String> subdomainVisits(String[] cpdomains) {
81-
Map<String, Integer> domains = new HashMap<>();
82-
for (String domain : cpdomains) {
83-
String[] t = domain.split(" ");
84-
int count = Integer.parseInt(t[0]);
85-
String[] subs = t[1].split("\\.");
86-
String cur = "";
87-
for (int i = subs.length - 1; i >= 0; --i) {
88-
cur = subs[i] + (i == subs.length - 1 ? "" : ".") + cur;
89-
domains.put(cur, domains.getOrDefault(cur, 0) + count);
89+
Map<String, Integer> cnt = new HashMap<>();
90+
for (String s : cpdomains) {
91+
int i = s.indexOf(" ");
92+
int v = Integer.parseInt(s.substring(0, i));
93+
for (; i < s.length(); ++i) {
94+
if (s.charAt(i) == ' ' || s.charAt(i) == '.') {
95+
String t = s.substring(i + 1);
96+
cnt.put(t, cnt.getOrDefault(t, 0) + v);
97+
}
9098
}
9199
}
92-
List<String> res = new ArrayList<>();
93-
domains.forEach((domain, count) -> { res.add(count + " " + domain); });
94-
return res;
100+
List<String> ans = new ArrayList<>();
101+
for (var e : cnt.entrySet()) {
102+
ans.add(e.getValue() + " " + e.getKey());
103+
}
104+
return ans;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
vector<string> subdomainVisits(vector<string>& cpdomains) {
115+
unordered_map<string, int> cnt;
116+
for (auto& s : cpdomains) {
117+
int i = s.find(' ');
118+
int v = stoi(s.substr(0, i));
119+
for (; i < s.size(); ++i) {
120+
if (s[i] == ' ' || s[i] == '.') {
121+
cnt[s.substr(i + 1)] += v;
122+
}
123+
}
124+
}
125+
vector<string> ans;
126+
for (auto& [s, v] : cnt) {
127+
ans.push_back(to_string(v) + " " + s);
128+
}
129+
return ans;
130+
}
131+
};
132+
```
133+
134+
### **Go**
135+
136+
```go
137+
func subdomainVisits(cpdomains []string) []string {
138+
cnt := map[string]int{}
139+
for _, s := range cpdomains {
140+
i := strings.IndexByte(s, ' ')
141+
v, _ := strconv.Atoi(s[:i])
142+
for ; i < len(s); i++ {
143+
if s[i] == ' ' || s[i] == '.' {
144+
cnt[s[i+1:]] += v
145+
}
146+
}
147+
}
148+
ans := make([]string, 0, len(cnt))
149+
for s, v := range cnt {
150+
ans = append(ans, strconv.Itoa(v)+" "+s)
95151
}
152+
return ans
96153
}
97154
```
98155

solution/0800-0899/0811.Subdomain Visit Count/README_EN.md

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,84 @@ For the subdomains, we will visit &quot;mail.com&quot; 900 + 1 = 901 times, &quo
5353
```python
5454
class Solution:
5555
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
56-
domains = Counter()
57-
for item in cpdomains:
58-
count, domain = item.split()
59-
count = int(count)
60-
subs = domain.split('.')
61-
for i in range(len(subs)):
62-
key = '.'.join(subs[i:])
63-
domains[key] += count
64-
return [f'{cnt} {domain}' for domain, cnt in domains.items()]
56+
cnt = Counter()
57+
for s in cpdomains:
58+
v = int(s[:s.index(' ')])
59+
for i, c in enumerate(s):
60+
if c in (' ', '.'):
61+
cnt[s[i + 1:]] += v
62+
return [f'{v} {s}' for s, v in cnt.items()]
6563
```
6664

6765
### **Java**
6866

6967
```java
7068
class Solution {
7169
public List<String> subdomainVisits(String[] cpdomains) {
72-
Map<String, Integer> domains = new HashMap<>();
73-
for (String domain : cpdomains) {
74-
String[] t = domain.split(" ");
75-
int count = Integer.parseInt(t[0]);
76-
String[] subs = t[1].split("\\.");
77-
String cur = "";
78-
for (int i = subs.length - 1; i >= 0; --i) {
79-
cur = subs[i] + (i == subs.length - 1 ? "" : ".") + cur;
80-
domains.put(cur, domains.getOrDefault(cur, 0) + count);
70+
Map<String, Integer> cnt = new HashMap<>();
71+
for (String s : cpdomains) {
72+
int i = s.indexOf(" ");
73+
int v = Integer.parseInt(s.substring(0, i));
74+
for (; i < s.length(); ++i) {
75+
if (s.charAt(i) == ' ' || s.charAt(i) == '.') {
76+
String t = s.substring(i + 1);
77+
cnt.put(t, cnt.getOrDefault(t, 0) + v);
78+
}
8179
}
8280
}
83-
List<String> res = new ArrayList<>();
84-
domains.forEach((domain, count) -> { res.add(count + " " + domain); });
85-
return res;
81+
List<String> ans = new ArrayList<>();
82+
for (var e : cnt.entrySet()) {
83+
ans.add(e.getValue() + " " + e.getKey());
84+
}
85+
return ans;
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
vector<string> subdomainVisits(vector<string>& cpdomains) {
96+
unordered_map<string, int> cnt;
97+
for (auto& s : cpdomains) {
98+
int i = s.find(' ');
99+
int v = stoi(s.substr(0, i));
100+
for (; i < s.size(); ++i) {
101+
if (s[i] == ' ' || s[i] == '.') {
102+
cnt[s.substr(i + 1)] += v;
103+
}
104+
}
105+
}
106+
vector<string> ans;
107+
for (auto& [s, v] : cnt) {
108+
ans.push_back(to_string(v) + " " + s);
109+
}
110+
return ans;
111+
}
112+
};
113+
```
114+
115+
### **Go**
116+
117+
```go
118+
func subdomainVisits(cpdomains []string) []string {
119+
cnt := map[string]int{}
120+
for _, s := range cpdomains {
121+
i := strings.IndexByte(s, ' ')
122+
v, _ := strconv.Atoi(s[:i])
123+
for ; i < len(s); i++ {
124+
if s[i] == ' ' || s[i] == '.' {
125+
cnt[s[i+1:]] += v
126+
}
127+
}
128+
}
129+
ans := make([]string, 0, len(cnt))
130+
for s, v := range cnt {
131+
ans = append(ans, strconv.Itoa(v)+" "+s)
86132
}
133+
return ans
87134
}
88135
```
89136

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<string> subdomainVisits(vector<string>& cpdomains) {
4+
unordered_map<string, int> cnt;
5+
for (auto& s : cpdomains) {
6+
int i = s.find(' ');
7+
int v = stoi(s.substr(0, i));
8+
for (; i < s.size(); ++i) {
9+
if (s[i] == ' ' || s[i] == '.') {
10+
cnt[s.substr(i + 1)] += v;
11+
}
12+
}
13+
}
14+
vector<string> ans;
15+
for (auto& [s, v] : cnt) {
16+
ans.push_back(to_string(v) + " " + s);
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func subdomainVisits(cpdomains []string) []string {
2+
cnt := map[string]int{}
3+
for _, s := range cpdomains {
4+
i := strings.IndexByte(s, ' ')
5+
v, _ := strconv.Atoi(s[:i])
6+
for ; i < len(s); i++ {
7+
if s[i] == ' ' || s[i] == '.' {
8+
cnt[s[i+1:]] += v
9+
}
10+
}
11+
}
12+
ans := make([]string, 0, len(cnt))
13+
for s, v := range cnt {
14+
ans = append(ans, strconv.Itoa(v)+" "+s)
15+
}
16+
return ans
17+
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
class Solution {
22
public List<String> subdomainVisits(String[] cpdomains) {
3-
Map<String, Integer> domains = new HashMap<>();
4-
for (String domain : cpdomains) {
5-
String[] t = domain.split(" ");
6-
int count = Integer.parseInt(t[0]);
7-
String[] subs = t[1].split("\\.");
8-
String cur = "";
9-
for (int i = subs.length - 1; i >= 0; --i) {
10-
cur = subs[i] + (i == subs.length - 1 ? "" : ".") + cur;
11-
domains.put(cur, domains.getOrDefault(cur, 0) + count);
3+
Map<String, Integer> cnt = new HashMap<>();
4+
for (String s : cpdomains) {
5+
int i = s.indexOf(" ");
6+
int v = Integer.parseInt(s.substring(0, i));
7+
for (; i < s.length(); ++i) {
8+
if (s.charAt(i) == ' ' || s.charAt(i) == '.') {
9+
String t = s.substring(i + 1);
10+
cnt.put(t, cnt.getOrDefault(t, 0) + v);
11+
}
1212
}
1313
}
14-
List<String> res = new ArrayList<>();
15-
domains.forEach((domain, count) -> { res.add(count + " " + domain); });
16-
return res;
14+
List<String> ans = new ArrayList<>();
15+
for (var e : cnt.entrySet()) {
16+
ans.add(e.getValue() + " " + e.getKey());
17+
}
18+
return ans;
1719
}
1820
}
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class Solution:
22
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
3-
domains = Counter()
4-
for item in cpdomains:
5-
count, domain = item.split()
6-
count = int(count)
7-
subs = domain.split('.')
8-
for i in range(len(subs)):
9-
key = '.'.join(subs[i:])
10-
domains[key] += count
11-
return [f'{cnt} {domain}' for domain, cnt in domains.items()]
3+
cnt = Counter()
4+
for s in cpdomains:
5+
v = int(s[:s.index(' ')])
6+
for i, c in enumerate(s):
7+
if c in (' ', '.'):
8+
cnt[s[i + 1:]] += v
9+
return [f'{v} {s}' for s, v in cnt.items()]

0 commit comments

Comments
 (0)