Subdomain Visit Count - Problem
Subdomain Visit Count
Imagine you're building an analytics system for website traffic! When someone visits a website like
๐
๐
๐
You're given an array of count-paired domains in the format
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.
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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code