|
| 1 | +/* |
| 2 | +Subdomain Visit Count |
| 3 | +Link: https://leetcode.com/problems/subdomain-visit-count/ |
| 4 | +
|
| 5 | +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. |
| 6 | +
|
| 7 | +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. |
| 8 | +
|
| 9 | +For example, "9001 discuss.leetcode.com" is a count-paired domain that indicates that discuss.leetcode.com was visited 9001 times. |
| 10 | +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. |
| 11 | +
|
| 12 | +Example 1: |
| 13 | +Input: cpdomains = ["9001 discuss.leetcode.com"] |
| 14 | +Output: ["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"] |
| 15 | +Explanation: We only have one website domain: "discuss.leetcode.com". |
| 16 | +As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times. |
| 17 | +
|
| 18 | +Example 2: |
| 19 | +Input: cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"] |
| 20 | +Output: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"] |
| 21 | +Explanation: We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. |
| 22 | +For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times. |
| 23 | +
|
| 24 | +Constraints: |
| 25 | +1 <= cpdomain.length <= 100 |
| 26 | +1 <= cpdomain[i].length <= 100 |
| 27 | +cpdomain[i] follows either the "repi d1i.d2i.d3i" format or the "repi d1i.d2i" format. |
| 28 | +repi is an integer in the range [1, 104]. |
| 29 | +d1i, d2i, and d3i consist of lowercase English letters. |
| 30 | + */ |
| 31 | +package com.raj; |
| 32 | + |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +public class SubdomainVisitCount { |
| 37 | + public static void main(String[] args) { |
| 38 | + // Initialization. |
| 39 | + String[] cpdomains = {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"}; |
| 40 | + List<String> ans = new ArrayList<>(); |
| 41 | + |
| 42 | + // Logic. |
| 43 | + for (int i = 0; i < cpdomains.length; i++) { |
| 44 | + String domain = cpdomains[i]; |
| 45 | + String[] splitter = domain.split(" "); |
| 46 | + int value = Integer.valueOf(splitter[0]); |
| 47 | + String onlyDomain = splitter[1]; |
| 48 | + /* |
| 49 | + The split("\\.") method splits the string at each occurrence of "." |
| 50 | + and returns an array of substrings. Since the . character has a special meaning |
| 51 | + in regular expressions, we need to escape it using the double backslash \\ in the split() method. |
| 52 | + */ |
| 53 | + String[] subDomainList = onlyDomain.split("\\."); |
| 54 | + StringBuilder tmp = new StringBuilder(); |
| 55 | + // Split with subdomain. i.e. google.mail.com |
| 56 | + for (int j = subDomainList.length - 1; j >= 0; j--) { |
| 57 | + boolean isFound = false; |
| 58 | + if (tmp.isEmpty()) { |
| 59 | + tmp.append(subDomainList[j]); |
| 60 | + } else { |
| 61 | + // Empty the StringBuilder by setting its length to 0 |
| 62 | + String newVal = subDomainList[j] + "." + tmp; |
| 63 | + tmp.setLength(0); |
| 64 | + tmp.append(newVal); |
| 65 | + } |
| 66 | + // Search if existing domain exist, Then increase the value else add the value. |
| 67 | + for (int k = 0; k < ans.size(); k++) { |
| 68 | + String domainvalue = ans.get(k); |
| 69 | + if (domainvalue.endsWith(" " + tmp)) { |
| 70 | + isFound = true; |
| 71 | + String[] newDomainSplit = domainvalue.split(" "); |
| 72 | + int newValue = Integer.valueOf(newDomainSplit[0]) + value; |
| 73 | + String newString = newValue + " " + newDomainSplit[1]; |
| 74 | + ans.set(k, newString); |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + if (!isFound) { |
| 79 | + ans.add(value + " " + tmp); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Display the result. |
| 85 | + System.out.println("The final sub domain visit counts are: " + ans); |
| 86 | + } |
| 87 | +} |
0 commit comments