Skip to content

Commit 4623498

Browse files
authored
Merge pull request #2089 from seinlin/929
Add 0929-unique-email-addresses.c
2 parents 67c5094 + c88daa7 commit 4623498

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Diff for: c/0929-unique-email-addresses.c

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Represent an element in a hash table.
2+
typedef struct Hash {
3+
char* key;
4+
UT_hash_handle hh;
5+
} Hash;
6+
7+
char *EmailToKey(char *email) {
8+
int len = strlen(email);
9+
char *key = (char*)malloc( len + 1);
10+
char *k = key;
11+
int domain = 0;
12+
int i;
13+
14+
for (i = 0; i < len; i++) {
15+
if (!domain && email[i] == '.') {
16+
continue;
17+
}
18+
if (email[i] == '+') {
19+
while(i < len && email[i] != '@') {
20+
i++;
21+
}
22+
}
23+
if (email[i] == '@') {
24+
domain = 1;
25+
}
26+
*k++ = email[i];
27+
}
28+
*k = '\0';
29+
30+
return key;
31+
}
32+
33+
int numUniqueEmails(char ** emails, int emailsSize){
34+
Hash *root = NULL;
35+
Hash *entry = NULL;
36+
char *key = NULL;
37+
int i;
38+
39+
for (i = 0; i < emailsSize; i++) {
40+
entry = NULL;
41+
key = EmailToKey(emails[i]);
42+
HASH_FIND_STR(root, key, entry);
43+
if (!entry) {
44+
entry = malloc(sizeof(Hash));
45+
entry->key = key;
46+
HASH_ADD_KEYPTR(hh, root, key, strlen(key), entry);
47+
} else {
48+
free(key);
49+
}
50+
}
51+
52+
return HASH_COUNT(root);
53+
}

0 commit comments

Comments
 (0)