Skip to content

Commit

Permalink
feat: add solutions to leetcode problem: No.0811
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Mar 20, 2021
1 parent 6a42f6c commit c24c47a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
33 changes: 31 additions & 2 deletions solution/0800-0899/0811.Subdomain Visit Count/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,44 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
domains = collections.Counter()
for item in cpdomains:
count, domain = item.split()
count = int(count)
subs = domain.split('.')
for i in range(len(subs)):
key = '.'.join(subs[i:])
domains[key] += count
return [f'{cnt} {domain}' for domain, cnt in domains.items()]
```

### **Java**

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

```java

class Solution {
public List<String> subdomainVisits(String[] cpdomains) {
Map<String, Integer> domains = new HashMap<>();
for (String domain : cpdomains) {
String[] t = domain.split(" ");
int count = Integer.parseInt(t[0]);
String[] subs = t[1].split("\\.");
String cur = "";
for (int i = subs.length - 1; i >= 0; --i) {
cur = subs[i] + (i == subs.length - 1 ? "" : ".") + cur;
domains.put(cur, domains.getOrDefault(cur, 0) + count);
}
}
List<String> res = new ArrayList<>();
domains.forEach((domain, count) -> {
res.add(count + " " + domain);
});
return res;
}
}
```

### **...**
Expand Down
33 changes: 31 additions & 2 deletions solution/0800-0899/0811.Subdomain Visit Count/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,42 @@ We will visit &quot;google.mail.com&quot; 900 times, &quot;yahoo.com&quot; 50 ti
### **Python3**

```python

class Solution:
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
domains = collections.Counter()
for item in cpdomains:
count, domain = item.split()
count = int(count)
subs = domain.split('.')
for i in range(len(subs)):
key = '.'.join(subs[i:])
domains[key] += count
return [f'{cnt} {domain}' for domain, cnt in domains.items()]
```

### **Java**

```java

class Solution {
public List<String> subdomainVisits(String[] cpdomains) {
Map<String, Integer> domains = new HashMap<>();
for (String domain : cpdomains) {
String[] t = domain.split(" ");
int count = Integer.parseInt(t[0]);
String[] subs = t[1].split("\\.");
String cur = "";
for (int i = subs.length - 1; i >= 0; --i) {
cur = subs[i] + (i == subs.length - 1 ? "" : ".") + cur;
domains.put(cur, domains.getOrDefault(cur, 0) + count);
}
}
List<String> res = new ArrayList<>();
domains.forEach((domain, count) -> {
res.add(count + " " + domain);
});
return res;
}
}
```

### **...**
Expand Down
20 changes: 20 additions & 0 deletions solution/0800-0899/0811.Subdomain Visit Count/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public List<String> subdomainVisits(String[] cpdomains) {
Map<String, Integer> domains = new HashMap<>();
for (String domain : cpdomains) {
String[] t = domain.split(" ");
int count = Integer.parseInt(t[0]);
String[] subs = t[1].split("\\.");
String cur = "";
for (int i = subs.length - 1; i >= 0; --i) {
cur = subs[i] + (i == subs.length - 1 ? "" : ".") + cur;
domains.put(cur, domains.getOrDefault(cur, 0) + count);
}
}
List<String> res = new ArrayList<>();
domains.forEach((domain, count) -> {
res.add(count + " " + domain);
});
return res;
}
}
11 changes: 11 additions & 0 deletions solution/0800-0899/0811.Subdomain Visit Count/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
domains = collections.Counter()
for item in cpdomains:
count, domain = item.split()
count = int(count)
subs = domain.split('.')
for i in range(len(subs)):
key = '.'.join(subs[i:])
domains[key] += count
return [f'{cnt} {domain}' for domain, cnt in domains.items()]

0 comments on commit c24c47a

Please sign in to comment.