




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.
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#define MAX_LENGTH 100
5
6// Define a struct for map entries
7typedef struct {
8    char domain[MAX_LENGTH];
9    int count;
10} DomainCountEntry;
11
12// Custom hashmap-like array
13typedef struct {
14    DomainCountEntry *entries;
15    int size;
16    int capacity;
17} DomainCountMap;
18
19void initMap(DomainCountMap *map, int capacity) {
20    map->entries = (DomainCountEntry *)malloc(capacity * sizeof(DomainCountEntry));
21    map->size = 0;
22    map->capacity = capacity;
23}
24
25void freeMap(DomainCountMap *map) {
26    free(map->entries);
27}
28
29int findIndex(DomainCountMap *map, const char *domain) {
30    for (int i = 0; i < map->size; i++) {
31        if (strcmp(map->entries[i].domain, domain) == 0) {
32            return i;
33        }
34    }
35    return -1;
36}
37
38void insertOrUpdate(DomainCountMap *map, const char *domain, int count) {
39    int index = findIndex(map, domain);
40    if (index == -1) {
41        // New entry
42        strcpy(map->entries[map->size].domain, domain);
43        map->entries[map->size].count = count;
44        map->size++;
45        if (map->size == map->capacity) {
46            // Increase capacity
47            map->capacity *= 2;
48            map->entries = (DomainCountEntry *)realloc(map->entries, map->capacity * sizeof(DomainCountEntry));
49        }
50    } else {
51        // Update existing count
52        map->entries[index].count += count;
53    }
54}
55
56void processDomains(const char **domains, int size, char ***result, int *resultSize) {
57    DomainCountMap map;
58    initMap(&map, 10);
59
60    for (int i = 0; i < size; i++) {
61        const char *cpdomain = domains[i];
62        int count;
63        char domain[MAX_LENGTH];
64        sscanf(cpdomain, "%d %s", &count, domain);
65
66        // Process domain
67        char *token = strtok(domain, ".");
68        char subdomain[MAX_LENGTH] = "";
69        char buffer[MAX_LENGTH];
70
71        while (token) {
72            if (*subdomain) {
73                snprintf(buffer, sizeof(buffer), "%s.%s", token, subdomain);
74            } else {
75                snprintf(buffer, sizeof(buffer), "%s", token);
76            }
77            strcpy(subdomain, buffer);
78            insertOrUpdate(&map, subdomain, count);
79
80            token = strtok(NULL, ".");
81        }
82    }
83
84    // Prepare result
85    *result = (char **)malloc(map.size * sizeof(char *));
86    *resultSize = map.size;
87    for (int i = 0; i < map.size; i++) {
88        (*result)[i] = (char *)malloc(MAX_LENGTH);
89        snprintf((*result)[i], MAX_LENGTH, "%d %s", map.entries[i].count, map.entries[i].domain);
90    }
91
92    freeMap(&map);
93}
94
95int main() {
96    const char *cpdomains[] = {"9001 discuss.leetcode.com", "900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
97    char **result;
98    int resultSize;
99
100    processDomains(cpdomains, 5, &result, &resultSize);
101
102    // Print results
103    for (int i = 0; i < resultSize; i++) {
104        printf("%s\n", result[i]);
105        free(result[i]);  // Free each result
106    }
107    free(result);  // Free result array
108
109    return 0;
110}The solution initializes a map as a dynamic array implemented using a struct array. The `processDomains` function processes each count-paired domain, extracts the count, and decomposes the domain into subdomains. For each subdomain, it updates the domain visit count in the map. Finally, it prepares the result in the specified format by iterating the map entries.
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.
1Implementing a Trie in C is nonA Trie implementation in C would involve struct definitions for nodes, containing a map to child nodes and an integer to tally counts.