Sponsored
Sponsored
In this approach, we'll use a set to store unique email addresses. We'll iterate over each email, split it into local and domain parts, then manipulate the local part by removing any characters after the first '+' and all periods '.' before rejoining with the domain. Finally, we'll add the standardized email to the set. The size of the set gives the count of unique emails.
Time Complexity: O(N * M) where N is the number of emails and M is the maximum length of an email address.
Space Complexity: O(N) due to the storage of unique emails in a set.
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4
5int numUniqueEmails(char **emails, int emailsSize) {
6 char *uniqueEmails[100];
7 int uniqueCount = 0;
8
9 for (int i = 0; i < emailsSize; i++) {
10 char *email = emails[i];
11 char local[100] = "";
12 char *domain = strchr(email, '@');
13 *domain++ = '\0';
14
15 char *plus = strchr(email, '+');
16 if (plus != NULL) {
17 *plus = '\0';
18 }
19
20 for (char *c = email; *c != '\0'; c++) {
21 if (*c != '.') {
22 strncat(local, c, 1);
23 }
24 }
25
26 strcat(local, "@");
27 strcat(local, domain);
28
29 int isDuplicate = 0;
30 for (int j = 0; j < uniqueCount; j++) {
31 if (strcmp(uniqueEmails[j], local) == 0) {
32 isDuplicate = 1;
33 break;
34 }
35 }
36 if (!isDuplicate) {
37 uniqueEmails[uniqueCount] = strdup(local);
38 uniqueCount++;
39 }
40 }
41 return uniqueCount;
42}
In C, we implement manual email parsing as C lacks advanced string operations. We create arrays for processed email and track them through a loop. For each email, we truncate at '@', check for '+', and eliminate '.'. We tally unique addresses using a 'uniqueEmails' array with manual duplication checks.
In this approach, instead of transforming emails directly using a set, we'll utilize arrays to manipulate the email parts. We'll maintain a boolean array for discovered unique emails and an index array to track up to where the email is unique. This efficient tracking helps us determine unique addresses without full storage, but more focused on transformations directly performed within the iteration loop.
Time Complexity: O(N * M), since per email processed once fully.
Space Complexity: O(N), enabling efficient tracking of unique addresses.
Again, C version emphasizing manual effort without advanced structures, taking a similar path yet making alterations in a dynamic segment using additional array-like modifications within loop logic.