Advertisement

Subdomain Visit Count - LeetCode 811 Solution

Subdomain Visit Count - Complete Solution Guide

Subdomain Visit Count is LeetCode problem 811, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A website domain "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com" , at the next level, we have "leetcode.com" and at the lowest level, "discuss.leetcode.com" . When we visit a domain like "discuss.leetcode.com" , we will also visit the parent domains "leetcode.com" and "com" implicitly. A count-paired domain is a domain that has one of the two formats "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 its

Detailed Explanation

The problem requires us to process an array of strings, where each string represents a domain and the number of visits to that domain. The goal is to calculate the total visits for each subdomain (e.g., 'discuss.leetcode.com', 'leetcode.com', 'com') and return the results as an array of strings in the format 'count domain'. A key concept is that a visit to a full domain also counts as a visit to all its parent subdomains.

Solution Approach

The solution uses a hash table (dictionary/map) to store the counts for each subdomain. The input array is iterated. For each domain, the count is extracted, and the domain is split into parts. Then, the code iterates through the domain parts, building the subdomain and updating the count in the hash table. Finally, the hash table is converted into the desired output format of an array of strings.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash table (domain_counts) to store the counts for each domain.
  2. Step 2: Iterate through the input array of count-paired domains (cpdomains).
  3. Step 3: For each cpdomain, split it into the count and the full domain.
  4. Step 4: Convert the count from string to integer.
  5. Step 5: Split the full domain into parts based on the '.' delimiter.
  6. Step 6: Iterate through the domain parts from right to left. Construct a subdomain by joining the domain parts.
  7. Step 7: Update the count for the subdomain in the domain_counts hash table.
  8. Step 8: After processing all cpdomains, iterate through the domain_counts hash table.
  9. Step 9: For each entry in the hash table, format the output string as 'count domain'.
  10. Step 10: Return the array of formatted strings.

Key Insights

  • Insight 1: Using a hash table (dictionary/map) to store the counts for each subdomain is efficient.
  • Insight 2: Iterating through the domain parts from right to left allows easy construction of the subdomains.
  • Insight 3: String manipulation and splitting domains based on '.' are crucial for extracting subdomains.

Complexity Analysis

Time Complexity: O(n*m)

Space Complexity: O(n*m)

Topics

This problem involves: Array, Hash Table, String, Counting.

Companies

Asked at: Karat, Peloton, Roblox, Wix.