[LeetCode] 811. 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.com
was visited9001
times.
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 <= 100
1 <= cpdomain[i].length <= 100
cpdomain[i]
follows either the"repi d1i.d2i.d3i"
format or the"repi d1i.d2i"
format.repi
is an integer in the range[1, 104]
.d1i
,d2i
, andd3i
consist of lowercase English letters.
子域名访问计数。
网站域名 "discuss.leetcode.com" 由多个子域名组成。顶级域名为 "com" ,二级域名为 "leetcode.com" ,最低一级为 "discuss.leetcode.com" 。当访问域名 "discuss.leetcode.com" 时,同时也会隐式访问其父域名 "leetcode.com" 以及 "com" 。
计数配对域名 是遵循 "rep d1.d2.d3" 或 "rep d1.d2" 格式的一个域名表示,其中 rep 表示访问域名的次数,d1.d2.d3 为域名本身。
例如,"9001 discuss.leetcode.com" 就是一个 计数配对域名 ,表示 discuss.leetcode.com 被访问了 9001 次。
给你一个 计数配对域名 组成的数组 cpdomains ,解析得到输入中每个子域名对应的 计数配对域名 ,并以数组形式返回。可以按 任意顺序 返回答案。来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/subdomain-visit-count
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意是给一个 string array,里面是一个带访问次数和域名的组合,要求分别计算每个域名被访问的次数。其格式为访问次数+空格+地址,例如:"9001 discuss.leetcode.com"。
思路是hashmap记录域名和他们分别出现的次数。细节如下,
首先因为给的有可能是不止一个域名的信息,所以for loop先分开每一个input,比如这样
[
"900 google.mail.com",
"50 yahoo.com",
"1 intel.mail.com",
"5 wiki.org"
]
然后再用string array分开每一个全域名的出现次数和全域名;接着再来从右向左遍历全域名,并且更新他们的出现次数,比如"900 google.mail.com",需要查看这三个域名。
com
mail.com
google.mail.com
注意一开始拿到com之后,下一个遍历到的域名是mail,需要跟com做一下string concatenation才能计数。
时间O(n)
空间O(n)
Java实现
1 class Solution { 2 public List<String> subdomainVisits(String[] cpdomains) { 3 Map<String, Integer> map = new HashMap<>(); 4 // 遍历每一个全域名信息 5 for (String cpdomain : cpdomains) { 6 // 使用空格分隔该信息 7 String[] arr = cpdomain.split(" "); 8 // 分割后第一部分为访问次数 9 int c = Integer.valueOf(arr[0]); 10 // 第二部分为全域名,将全域名使用.进行分割 11 String[] domainArr = arr[1].split("\\."); 12 // 子域名 13 String sub = ""; 14 // 从全域名的最后一段组合子域名 15 for (int i = domainArr.length - 1; i >= 0; i--) { 16 if ("".equals(sub)) { 17 sub = domainArr[i]; 18 } else { 19 sub = domainArr[i] + "." + sub; 20 } 21 // 更新该子域名出现次数 22 int count = map.getOrDefault(sub, 0) + c; 23 map.put(sub, count); 24 } 25 } 26 // 将map中的数据按照题目要求添加至返回List 27 List<String> res = new ArrayList<>(); 28 for (String key : map.keySet()) { 29 res.add(map.get(key) + " " + key); 30 } 31 return res; 32 } 33 }