Skip to content

Commit c24c47a

Browse files
committed
feat: add solutions to leetcode problem: No.0811
1 parent 6a42f6c commit c24c47a

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

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

+31-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,44 @@
5252
<!-- 这里可写当前语言的特殊实现逻辑 -->
5353

5454
```python
55-
55+
class Solution:
56+
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
57+
domains = collections.Counter()
58+
for item in cpdomains:
59+
count, domain = item.split()
60+
count = int(count)
61+
subs = domain.split('.')
62+
for i in range(len(subs)):
63+
key = '.'.join(subs[i:])
64+
domains[key] += count
65+
return [f'{cnt} {domain}' for domain, cnt in domains.items()]
5666
```
5767

5868
### **Java**
5969

6070
<!-- 这里可写当前语言的特殊实现逻辑 -->
6171

6272
```java
63-
73+
class Solution {
74+
public List<String> subdomainVisits(String[] cpdomains) {
75+
Map<String, Integer> domains = new HashMap<>();
76+
for (String domain : cpdomains) {
77+
String[] t = domain.split(" ");
78+
int count = Integer.parseInt(t[0]);
79+
String[] subs = t[1].split("\\.");
80+
String cur = "";
81+
for (int i = subs.length - 1; i >= 0; --i) {
82+
cur = subs[i] + (i == subs.length - 1 ? "" : ".") + cur;
83+
domains.put(cur, domains.getOrDefault(cur, 0) + count);
84+
}
85+
}
86+
List<String> res = new ArrayList<>();
87+
domains.forEach((domain, count) -> {
88+
res.add(count + " " + domain);
89+
});
90+
return res;
91+
}
92+
}
6493
```
6594

6695
### **...**

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

+31-2
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,42 @@ We will visit &quot;google.mail.com&quot; 900 times, &quot;yahoo.com&quot; 50 ti
6767
### **Python3**
6868

6969
```python
70-
70+
class Solution:
71+
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
72+
domains = collections.Counter()
73+
for item in cpdomains:
74+
count, domain = item.split()
75+
count = int(count)
76+
subs = domain.split('.')
77+
for i in range(len(subs)):
78+
key = '.'.join(subs[i:])
79+
domains[key] += count
80+
return [f'{cnt} {domain}' for domain, cnt in domains.items()]
7181
```
7282

7383
### **Java**
7484

7585
```java
76-
86+
class Solution {
87+
public List<String> subdomainVisits(String[] cpdomains) {
88+
Map<String, Integer> domains = new HashMap<>();
89+
for (String domain : cpdomains) {
90+
String[] t = domain.split(" ");
91+
int count = Integer.parseInt(t[0]);
92+
String[] subs = t[1].split("\\.");
93+
String cur = "";
94+
for (int i = subs.length - 1; i >= 0; --i) {
95+
cur = subs[i] + (i == subs.length - 1 ? "" : ".") + cur;
96+
domains.put(cur, domains.getOrDefault(cur, 0) + count);
97+
}
98+
}
99+
List<String> res = new ArrayList<>();
100+
domains.forEach((domain, count) -> {
101+
res.add(count + " " + domain);
102+
});
103+
return res;
104+
}
105+
}
77106
```
78107

79108
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
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);
12+
}
13+
}
14+
List<String> res = new ArrayList<>();
15+
domains.forEach((domain, count) -> {
16+
res.add(count + " " + domain);
17+
});
18+
return res;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
3+
domains = collections.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()]

0 commit comments

Comments
 (0)