Problem Description
Given an array of count-paired domains, return an array of the count-paired domains of each subdomain in the input. A count-paired domain is in the format "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.
Key Insights
- Each domain visit also counts for its parent domains.
- We need to derive visits for all levels of subdomains.
- A hash map can efficiently store and accumulate visit counts for each subdomain.
Space and Time Complexity
Time Complexity: O(N * M), where N is the number of count-paired domains and M is the maximum number of subdomains derived from a single count-paired domain. Space Complexity: O(S), where S is the number of unique subdomains.
Solution
To solve this problem, we can use a hash map (or dictionary) to store the total visit counts for each subdomain. For each count-paired domain in the input, we will:
- Split the string into the number of visits (rep) and the domain.
- Split the domain into its components (subdomains).
- For each component, we will build the full subdomain from the current component to the top-level domain, accumulating the visit counts in the hash map.
- Finally, we will format the results from the hash map back into the required output format.