aboutsummaryrefslogtreecommitdiff
path: root/subdomain_visit_count.c
blob: 2e9bc1c33425ca7e0a208f24858af734303f1923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 811. Subdomain Visit Count
#include "leetcode.h"

/*
 * a website domain "discuss.leetcode.com" consists of various subdomains. at
 * the level, we have "com" at the next level, we have "leetcode.com", and the
 * lest level, "discuss.leetcode.com". when we visit a domain like the above, 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 itself. given an array of count paird
 * domains 'cpdomains' return an array of the count pared domains of each
 * subdomain in the input. you may return the answer in any order.
 */

#define STR_LEN 110

typedef struct {
  char domain[STR_LEN];
  int cnt;
  UT_hash_handle hh;
} hash;

void add_domain(hash **webs, char name[STR_LEN], int num) {
  hash *s;
  HASH_FIND_STR(*webs, name, s);
  if (s)
    s->cnt += num;
  else {
    s = (hash *)malloc(sizeof(hash));
    s->cnt = num;
    strcpy(s->domain, name);
    HASH_ADD_STR(*webs, domain, s);
  }
}

char **subdomainVisits(char **cpdomains, int cpdomainsSize, int *returnSize) {
  hash *webs = NULL, *s, *tmp;
  char **ans = (char **)malloc(cpdomainsSize * 3 * sizeof(char)),
       cut[STR_LEN] = "";
  for (int i = 0; i < cpdomainsSize * 3; i++)
    ans[i] = (char *)malloc(STR_LEN * sizeof(char));
  for (int i = 0; i < cpdomainsSize; i++) {
    int n = strlen(cpdomains[i]), j = 0, k = 0;
    while (cpdomains[i][j] != '\0') {
      int num, l;
      if (cpdomains[i][j] == ' ' && !k) {
        k = j;
        memcpy(cut, cpdomains[i], k);
        num = atoi(cut);
        l = j + 1;
        memcpy(cut, cpdomains[i] + l, n - l + 1);
        add_domain(&webs, cut, num);
      }
      if (cpdomains[i][j] == '.') {
        l = j + 1;
        memcpy(cut, cpdomains[i] + l, n - l + 1);
        add_domain(&webs, cut, num);
      }
      j++;
    }
  }
  int i = 0;
  HASH_ITER(hh, webs, s, tmp) {
    sprintf(cut, "%d %s", s->cnt, s->domain);
    strcpy(ans[i], cut);
    i++;
  }
  *returnSize = i;
  return ans;
}

int main() {
  char *cpd1[] = {"9001 discuss.leetcode.com"},
       *cpd2[] = {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com",
                  "5 wiki.org"};
  int rs1, rs2;
  char **sv1 = subdomainVisits((char **)cpd1, ARRAY_SIZE(cpd1), &rs1);
  char **sv2 = subdomainVisits((char **)cpd2, ARRAY_SIZE(cpd2), &rs2);
  for (int i = 0; i < rs1; i++)
    printf("%s ", sv1[i]); // expect: ["9001 leetcode.com","9001
                           // discuss.leetcode.com","9001 com"]
  printf("\n");
  for (int i = 0; i < rs2; i++)
    printf("%s ", sv2[i]); // expect: ["901 mail.com","50 yahoo.com","900
                           // google.mail.com","5 wiki.org","5 org","1
                           // intel.mail.com","951 com"]
  printf("\n");
}