
Sponsored
Sponsored
We will use a hashmap (or dictionary) to record the visit count of each subdomain as we process each count-paired domain in the input list. By splitting each domain into its subdomains, starting from the rightmost part, we accumulate the visits to each subdomain in the map.
Time Complexity: O(n * m) where n is the number of domains and m is the average number of subcomponents of a domain. Space Complexity: O(n * m) for storing subdomains in the map.
1using System;
2using System.Collections.Generic;
3
4class Solution {
5 public static IList<string> SubdomainVisits(string[] cpdomains) {
6 Dictionary<string, int> counts = new Dictionary<string, int>();
7 foreach (string cpdomain in cpdomains) {
8 string[] parts = cpdomain.Split(' ');
9 int count = int.Parse(parts[0]);
10 string domain = parts[1];
11
12 while (true) {
13 if (counts.ContainsKey(domain)) {
14 counts[domain] += count;
15 } else {
16 counts[domain] = count;
17 }
18
19 int dotIndex = domain.IndexOf('.');
20 if (dotIndex < 0) break;
21 domain = domain.Substring(dotIndex + 1);
22 }
23 }
24
25 List<string> result = new List<string>();
26 foreach (KeyValuePair<string, int> entry in counts) {
27 result.Add(entry.Value + " " + entry.Key);
28 }
29 return result;
30 }
31
32 public static void Main(string[] args) {
33 string[] cpdomains = {"9001 discuss.leetcode.com", "900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
34 IList<string> result = SubdomainVisits(cpdomains);
35 foreach (string res in result) {
36 Console.WriteLine(res);
37 }
38 }
39}This C# program processes each domain, storing subdomain visit counts in a Dictionary. As subdomains are updated with their counts, they get added to the result list which is finally printed.
We can also implement the solution using a trie structure to track domains. Each subcomponent of a domain traverses down nodes in the trie, updating the count at each node. This approach leverages the natural hierarchy of domains efficiently through a tree structure.
Time Complexity: O(n * m), n domains and m depth. Space Complexity: Higher space use due to node allocations.
1#include <iostream>
#include <map>
#include <vector>
#include <sstream>
using namespace std;
struct TrieNode {
map<string, TrieNode*> children;
int count = 0;
};
void insert(TrieNode* root, vector<string>& tokens, int count) {
TrieNode* node = root;
for (auto it = tokens.rbegin(); it != tokens.rend(); ++it) {
if (!node->children[*it])
node->children[*it] = new TrieNode();
node = node->children[*it];
node->count += count;
}
}
void dfs(TrieNode* node, string current, vector<string>& result) {
if (node->count > 0) {
result.push_back(to_string(node->count) + " " + current);
}
for (auto& child : node->children) {
dfs(child.second, child.first + (current.empty() ? "" : ".") + current, result);
}
}
vector<string> subdomainVisits(vector<string>& cpdomains) {
TrieNode* root = new TrieNode();
for (auto& domain : cpdomains) {
stringstream ss(domain);
int count;
ss >> count;
string dom;
ss >> dom;
vector<string> tokens;
stringstream ss_tokens(dom);
string token;
while (getline(ss_tokens, token, '.')) {
tokens.push_back(token);
}
insert(root, tokens, count);
}
vector<string> result;
dfs(root, "", result);
return result;
}
int main() {
vector<string> cpdomains = {"9001 discuss.leetcode.com", "900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
vector<string> result = subdomainVisits(cpdomains);
for (string& s : result) {
cout << s << endl;
}
return 0;
}This solution leverages a trie to efficiently store subdomain structures. Each visit parses the input domain into segments, which are inserted into the trie, incrementing counts at each node. A recursive DFS then builds results by aggregating counts.