forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to leetcode problem: No.0811
- Loading branch information
Showing
4 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
solution/0800-0899/0811.Subdomain Visit Count/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()] |