原文链接: https://leetcode-cn.com/problems/subdomain-visit-count
英文原文
A website domain "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com" and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.
A count-paired domain is a domain that has one of the two formats "rep d1.d2.d3" or "rep d1.d2" where rep is the number of visits to the domain and d1.d2.d3 is the domain itself.
- For example,
"9001 discuss.leetcode.com"is a count-paired domain that indicates thatdiscuss.leetcode.comwas visited9001times.
Given an array of count-paired domains cpdomains, return an array of the count-paired domains of each subdomain in the input. You may return the answer in any order.
Example 1:
Input: cpdomains = ["9001 discuss.leetcode.com"] Output: ["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"] Explanation: We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.
Example 2:
Input: cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"] Output: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"] Explanation: We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
Constraints:
1 <= cpdomain.length <= 1001 <= cpdomain[i].length <= 100cpdomain[i]follows either the"repi d1i.d2i.d3i"format or the"repi d1i.d2i"format.repiis an integer in the range[1, 104].d1i,d2i, andd3iconsist of lowercase English letters.
中文题目
一个网站域名,如"discuss.leetcode.com",包含了多个子域名。作为顶级域名,常用的有"com",下一级则有"leetcode.com",最低的一级为"discuss.leetcode.com"。当我们访问域名"discuss.leetcode.com"时,也同时访问了其父域名"leetcode.com"以及顶级域名 "com"。
给定一个带访问次数和域名的组合,要求分别计算每个域名被访问的次数。其格式为访问次数+空格+地址,例如:"9001 discuss.leetcode.com"。
接下来会给出一组访问次数和域名组合的列表cpdomains 。要求解析出所有域名的访问次数,输出格式和输入格式相同,不限定先后顺序。
示例 1: 输入: ["9001 discuss.leetcode.com"] 输出: ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"] 说明: 例子中仅包含一个网站域名:"discuss.leetcode.com"。按照前文假设,子域名"leetcode.com"和"com"都会被访问,所以它们都被访问了9001次。
示例 2 输入: ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"] 输出: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"] 说明: 按照假设,会访问"google.mail.com" 900次,"yahoo.com" 50次,"intel.mail.com" 1次,"wiki.org" 5次。 而对于父域名,会访问"mail.com" 900+1 = 901次,"com" 900 + 50 + 1 = 951次,和 "org" 5 次。
注意事项:
-
cpdomains的长度小于100。 - 每个域名的长度小于
100。 - 每个域名地址包含一个或两个"."符号。
- 输入中任意一个域名的访问次数都小于
10000。
通过代码
官方题解
哈希映射:
对于包含一个 . 的域名 x.y,我们需要统计的是 x.y 和 y;对于包含两个 . 的域名 a.b.c,我们需要统计的是 a.b.c,b.c 和 c。在统计这些字符串时,我们可以使用哈希映射(HashMap)。统计结束之后,我们遍历哈希映射并输出结果。
[sol1]class Solution { public List<String> subdomainVisits(String[] cpdomains) { Map<String, Integer> counts = new HashMap(); for (String domain: cpdomains) { String[] cpinfo = domain.split("\\s+"); String[] frags = cpinfo[1].split("\\."); int count = Integer.valueOf(cpinfo[0]); String cur = ""; for (int i = frags.length - 1; i >= 0; --i) { cur = frags[i] + (i < frags.length - 1 ? "." : "") + cur; counts.put(cur, counts.getOrDefault(cur, 0) + count); } } List<String> ans = new ArrayList(); for (String dom: counts.keySet()) ans.add("" + counts.get(dom) + " " + dom); return ans; } }
[sol1]class Solution(object): def subdomainVisits(self, cpdomains): ans = collections.Counter() for domain in cpdomains: count, domain = domain.split() count = int(count) frags = domain.split('.') for i in xrange(len(frags)): ans[".".join(frags[i:])] += count return ["{} {}".format(ct, dom) for dom, ct in ans.items()]
复杂度分析
时间复杂度:$O(N)$,其中 $N$ 是数组
cpdomains的长度,这里假设cpdomains中每个元素的长度都是常数级别的。空间复杂度:$O(N)$,用于存储哈希映射。
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 16194 | 22964 | 70.5% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|