|
| 1 | +package io.github.dunwu.algorithm.hashtable; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +/* |
| 9 | +https://leetcode.com/problems/subdomain-visit-count/ |
| 10 | +
|
| 11 | +A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the |
| 12 | +next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". |
| 13 | +When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and |
| 14 | +"com" implicitly. |
| 15 | +
|
| 16 | +Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), |
| 17 | +followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com". |
| 18 | +
|
| 19 | +We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format |
| 20 | +as the input, and in any order), that explicitly counts the number of visits to each subdomain. |
| 21 | +
|
| 22 | +Example 1: |
| 23 | +Input: |
| 24 | +["9001 discuss.leetcode.com"] |
| 25 | +Output: |
| 26 | +["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"] |
| 27 | +Explanation: |
| 28 | +We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" |
| 29 | +will also be visited. So they will all be visited 9001 times. |
| 30 | +
|
| 31 | +Example 2: |
| 32 | +Input: |
| 33 | +["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"] |
| 34 | +Output: |
| 35 | +["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"] |
| 36 | +Explanation: |
| 37 | +We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. |
| 38 | +For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times. |
| 39 | +
|
| 40 | +Notes: |
| 41 | +
|
| 42 | +The length of cpdomains will not exceed 100. |
| 43 | +The length of each domain name will not exceed 100. |
| 44 | +Each address will have either 1 or 2 "." characters. |
| 45 | +The input count in any count-paired domain will not exceed 10000. |
| 46 | +The answer output can be returned in any order. |
| 47 | +*/ |
| 48 | +public class SubdomainVisitCount { |
| 49 | + |
| 50 | + public List<String> subdomainVisits(String[] cpdomains) { |
| 51 | + List<String> result = new ArrayList<>(); |
| 52 | + Map<String, Integer> map = new HashMap<>(); // key: subdomain, value: frequency |
| 53 | + StringBuilder resultStringBuilder = new StringBuilder(); |
| 54 | + |
| 55 | + for (String cpdomain : cpdomains) { |
| 56 | + int indexSpace = cpdomain.indexOf(' '); |
| 57 | + int numClicks = Integer.parseInt(cpdomain.substring(0, indexSpace)); |
| 58 | + String domain = cpdomain.substring(indexSpace + 1); |
| 59 | + resultStringBuilder.setLength(0); |
| 60 | + resultStringBuilder.append(domain); |
| 61 | + while (true) { |
| 62 | + map.put(resultStringBuilder.toString(), map.getOrDefault(resultStringBuilder.toString(), 0) + numClicks); |
| 63 | + int dotPosition = resultStringBuilder.indexOf("."); |
| 64 | + if (dotPosition == -1) |
| 65 | + break; |
| 66 | + resultStringBuilder.delete(0, dotPosition + 1); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + for (String domain : map.keySet()) |
| 71 | + result.add(map.get(domain) + " " + domain); |
| 72 | + |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + public static void main(String[] args) { |
| 77 | + SubdomainVisitCount tmpl = new SubdomainVisitCount(); |
| 78 | + |
| 79 | + String[] s1 = new String[]{"9001 discuss.leetcode.com"}; |
| 80 | + String[] s2 = new String[]{"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"}; |
| 81 | + tmpl.subdomainVisits(s1); |
| 82 | + tmpl.subdomainVisits(s2); |
| 83 | + } |
| 84 | +} |
0 commit comments