Subdomain Visit Count - Problem
Subdomain Visit Count

Imagine you're building an analytics system for website traffic! When someone visits a website like discuss.leetcode.com, they're actually visiting multiple domains at once:

๐ŸŒ discuss.leetcode.com (the full domain)
๐ŸŒ leetcode.com (parent domain)
๐ŸŒ com (top-level domain)

You're given an array of count-paired domains in the format "count domain". For example, "9001 discuss.leetcode.com" means that discuss.leetcode.com was visited 9001 times.

Your task: Calculate the total visit count for each subdomain (including parent domains) and return them as count-paired strings. The order doesn't matter!

Think of it like a tree structure where visiting a leaf node also counts as visiting all its parent nodes.

Input & Output

example_1.py โ€” Basic Case
$ Input: ["9001 discuss.leetcode.com"]
โ€บ Output: ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
๐Ÿ’ก Note: Visiting discuss.leetcode.com 9001 times means we also visit its parent domains leetcode.com and com 9001 times each.
example_2.py โ€” Multiple Domains
$ Input: ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com"]
โ€บ Output: ["901 mail.com", "50 yahoo.com", "900 google.mail.com", "1 intel.mail.com", "951 com"]
๐Ÿ’ก Note: mail.com gets 900+1=901 visits, com gets 900+50+1=951 visits from all the different domains.
example_3.py โ€” Single Level Domain
$ Input: ["1 com"]
โ€บ Output: ["1 com"]
๐Ÿ’ก Note: A top-level domain like com has no parent domains, so only itself is counted.

Constraints

  • 1 โ‰ค cpdomains.length โ‰ค 100
  • 1 โ‰ค cpdomains[i].length โ‰ค 100
  • cpdomains[i] follows either the format "repi d1i.d2i.d3i" or "repi d1i.d2i"
  • repi is an integer in the range [1, 104]
  • d1i, d2i, and d3i consist of lowercase English letters only

Visualization

Tap to expand
comCount: 9001leetcode.comCount: 9001yahoo.comCount: 50discuss.leetcode.comCount: 9001Hash Map Updates (O(1) each)com += 9001 | leetcode.com += 9001 | discuss.leetcode.com += 9001
Understanding the Visualization
1
Parse Input
Extract visit count and full domain name
2
Generate Subdomains
Split domain and create all parent domain combinations
3
Update Counters
Add visit count to each subdomain using hash map for O(1) access
4
Format Output
Convert hash map entries back to count-paired strings
Key Takeaway
๐ŸŽฏ Key Insight: Use hash map for O(1) subdomain lookups, processing each domain only once while updating all its parent domains simultaneously!
Asked in
Google 45 Amazon 38 Meta 22 Microsoft 18
42.6K Views
Medium Frequency
~15 min Avg. Time
1.8K 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