Leetcode 811: Subdomain Visit Count

grid47
grid47
Exploring patterns and algorithms
Aug 17, 2024 6 min read

A set of web domains where subdomains are counted, with each subdomain glowing softly as it’s identified.
Solution to LeetCode 811: Subdomain Visit Count Problem

You are given a list of count-paired domains, where each entry consists of a number followed by a domain name. The number represents the number of visits to that domain. A domain may also have subdomains, and visiting a subdomain also counts as visiting its parent domains. Your task is to return the count of visits for each domain and its subdomains.
Problem
Approach
Steps
Complexity
Input: You will be provided with a list of strings where each string contains a visit count and a domain name. The format is 'count domain'. Each domain name can have subdomains separated by dots.
Example: Input: cpdomains = ['100 xyz.com', '200 abc.xyz.com']
Constraints:
• 1 <= cpdomain.length <= 100
• 1 <= cpdomain[i].length <= 100
• cpdomain[i] follows the 'count domain' format.
• The domains are composed of lowercase English letters.
Output: The output should be a list of strings, each string showing the total count of visits for a domain or subdomain, in the format 'count domain'.
Example: Output: ['100 xyz.com', '200 abc.xyz.com', '300 com']
Constraints:
• The result should be returned in any order.
Goal: The task is to determine how many times each domain and its subdomains have been visited, accounting for the subdomains' visits as well.
Steps:
• Parse the input string to extract the number of visits and the domain name.
• For each domain, also count its parent domains by iterating through the subdomains.
• Store the total visit count for each domain and its subdomains in a map or dictionary.
• Finally, return the list of counts in the required format.
Goal: Ensure that your solution can handle the maximum number of domains and counts efficiently.
Steps:
• The input size is small enough that the solution should run efficiently within the provided constraints.
Assumptions:
• The input data format is valid and follows the expected pattern.
• Each domain and subdomain consists only of lowercase letters and dot separators.
Input: Input: cpdomains = ['100 xyz.com', '200 abc.xyz.com']
Explanation: In this example, 'xyz.com' is visited 100 times, 'abc.xyz.com' is visited 200 times, and 'com' is visited a total of 300 times (100 from 'xyz.com' and 200 from 'abc.xyz.com').

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus