![]() |
VOOZH | about |
A website domain "write.geeksforgeeks.org" consists of various subdomains. At the top level, we have "org", at the next level, we have "geeksforgeeks.org" and at the lowest level, "write.geeksforgeeks.org". When we visit a domain like "write.geeksforgeeks.org", we will also visit the parent domains "geeksforgeeks.org" and "org".
A count-paired domain is a domain that has one of the two formats "cnt d1.d2.d3" or "cnt d1.d2" where cnt is the number of visits to the domain and d1.d2.d3 is the domain itself. For example, "1000 write.geeksforgeeks.org" is a count-paired domain that indicates that write.geeksforgeeks.org was visited 1000 times.
Given an array of count-paired domains cpdomains[], return an array of the count-paired domains of each subdomain in the input. Return the answer in any order.
Examples:
Input: cpdomains = ["1000 geeksforgeeks.org"]
Output: ["1000 geeksforgeeks.org", "1000 org"]
Explanation: We only have one website domain: "geeksforgeeks.org". The subdomain "org" will also be visited. So, both will be visited 1000 times.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"]
Explanation: We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
Approach: To solve the problem, follow the below idea:
The problem can be solved using hashing. Use a hashmap to store all the domains and subdomains. Iterate over all the count-paired domains, and for each count-paired domain, get the count by extracting the string till the first space. After the first space extract the domain and subdomain and increment their frequency by adding count to the hash map.
Step-by-step algorithm:
Below is the implementation of the algorithm:
951 com 900 google.mail.com 1 intel.mail.com 901 mail.com 5 org 5 wiki.org 50 yahoo.com
Time Complexity: O(N * logN), where N is the number of count-paired domains in input.
Auxiliary Space: O(N)