|
| 1 | +--- |
| 2 | +id: subdomain-visit-count |
| 3 | +title: Subdomain Visit Count |
| 4 | +sidebar_label: 811- Subdomain Visit Count |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - String |
| 8 | + - Hash Table |
| 9 | +description: Given an array of count-paired domains, return an array showing the total visit counts for each subdomain and its parent domains. |
| 10 | +sidebar_position: 0811 |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +For example, `"9001 discuss.leetcode.com"` is a count-paired domain that indicates that `discuss.leetcode.com` was visited `9001` times. |
| 20 | +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. |
| 21 | + |
| 22 | +### Example 1 |
| 23 | + |
| 24 | +- **Input:** `cpdomains = ["9001 discuss.leetcode.com"]` |
| 25 | +- **Output:** `["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]` |
| 26 | +- **Explanation:** `We only have one website domain: "discuss.leetcode.com". |
| 27 | +As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.` |
| 28 | + |
| 29 | + |
| 30 | +### Constraints |
| 31 | + |
| 32 | +- `1 <= cpdomain.length <= 100` |
| 33 | +- `1 <= cpdomain[i].length <= 100` |
| 34 | + |
| 35 | +## Approach |
| 36 | + |
| 37 | +Just store each & every valid substring in a HashMap with its frequency count. |
| 38 | +Update the frequency count whenever the same string is encounterd. |
| 39 | +Finally, create a new string using each key-value pair of the HashMap & add that new string to the final list. |
| 40 | + |
| 41 | +### Solution Code |
| 42 | + |
| 43 | +#### Java |
| 44 | +```Java |
| 45 | +class Solution { |
| 46 | + HashMap<String, Integer> hm= new HashMap<>(); |
| 47 | + |
| 48 | + public List<String> subdomainVisits(String[] cpdomains) { |
| 49 | + List<String> result= new ArrayList<>(); |
| 50 | + |
| 51 | + for(String s: cpdomains){ |
| 52 | + addToList(s); |
| 53 | + } |
| 54 | + |
| 55 | + for(String s: hm.keySet()){ |
| 56 | + StringBuilder sb= new StringBuilder( Integer.toString(hm.get(s)) ); |
| 57 | + sb.append(" "); |
| 58 | + sb.append( s ); |
| 59 | + result.add( sb.toString() ); |
| 60 | + } |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + public void addToList(String s){ |
| 65 | + String[] split1= s.split(" "); |
| 66 | + int n= Integer.parseInt(split1[0]); |
| 67 | + String[] split2= split1[1].split("\\."); |
| 68 | + int l= split2.length; |
| 69 | + |
| 70 | + for(int i=0; i<l; i++){ |
| 71 | + StringBuilder sb= new StringBuilder(); |
| 72 | + int index= i; |
| 73 | + |
| 74 | + while(index < l){ |
| 75 | + sb.append( split2[index] ); |
| 76 | + |
| 77 | + if(index != l-1) |
| 78 | + sb.append( "." ); |
| 79 | + index++; |
| 80 | + } |
| 81 | + String str= sb.toString(); |
| 82 | + hm.put( str, hm.getOrDefault( str, 0 )+n); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +- Time Complexity |
| 90 | +The time complexity is O(N⋅L2). |
| 91 | + |
| 92 | +- Space Complexity |
| 93 | +The space complexity is O(1). |
0 commit comments