Subdomain Visit Count - Problem

A website domain like "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 that discuss.leetcode.com was visited 9001 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.

Input & Output

Example 1 — Basic Three-Level Domain
$ Input: cpdomains = ["9001 discuss.leetcode.com"]
Output: ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
💡 Note: Visiting "discuss.leetcode.com" contributes 9001 visits to itself and all parent domains: "leetcode.com" and "com"
Example 2 — Multiple Domains with Overlap
$ 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"]
💡 Note: Multiple domains contribute to shared parent domains: "mail.com" gets 900+1=901 visits, "com" gets 900+50+1=951 visits
Example 3 — Two-Level Domain Only
$ Input: cpdomains = ["100 facebook.com"]
Output: ["100 facebook.com", "100 com"]
💡 Note: Two-level domain generates only itself and top-level domain "com"

Constraints

  • 1 ≤ cplists.length ≤ 100
  • 1 ≤ cplists[i].length ≤ 100
  • cplists[i] follows either the format "rep d1.d2.d3" or "rep d1.d2"
  • rep is an integer in the range [1, 104]
  • d1, d2, d3 consist of lowercase English letters

Visualization

Tap to expand
Subdomain Visit Count INPUT cpdomains = ["9001 discuss.leetcode.com"] Domain Hierarchy: discuss.leetcode.com (Full domain - Level 3) leetcode.com (Parent - Level 2) com (Top level - Level 1) Visit Count: 9001 applies to ALL levels ALGORITHM (Hash Map) 1 Parse Input Split: count + domain 2 Extract Subdomains Split by "." character 3 Update Hash Map Add count to each level 4 Format Output Convert map to array Hash Map (domain -> count) "discuss.leetcode.com" 9001 "leetcode.com" 9001 "com" 9001 Key Value FINAL RESULT Output Array: "9001 discuss.leetcode.com" OK "9001 leetcode.com" OK "9001 com" OK Summary Total domains: 3 Each domain visited: 9001x Hierarchy preserved Time: O(n * m) n=domains, m=avg levels Key Insight: When visiting "a.b.c", we implicitly visit "b.c" and "c" as well. Use a Hash Map to track cumulative counts for each subdomain. For each domain, iterate from the full domain to the top-level domain, adding the visit count to each level in the hash map. TutorialsPoint - Subdomain Visit Count | Hash Map Approach
Asked in
Amazon 15 Google 12 Facebook 8 Microsoft 6
89.2K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen