LeetCode //C - 929. Unique Email Addresses
This problem requires counting the number of distinct email addresses that actually received the emails. The processing rules include:The period ('.') in the locale name is ignored; for example, "alic
929. Unique Email Addresses
Every valid email consists of a local name and a domain name, separated by the ‘@’ sign. Besides lowercase letters, the email may contain one or more ‘.’ or ‘+’.
- For example, in “alice@leetcode.com”, “alice” is the local name, and “leetcode.com” is the domain name.
If you add periods ‘.’ between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.
- For example, “alice.z@leetcode.com” and “alicez@leetcode.com” forward to the same email address.
If you add a plus ‘+’ in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.
- For example, “m.y+name@email.com” will be forwarded to “my@email.com”.
It is possible to use both of these rules at the same time.
Given an array of strings emails where we send one email to each emails[i], return the number of different addresses that actually receive mails.
Example 1:
Input: emails = [“test.email+alex@leetcode.com”,“test.e.mail+bob.cathy@leetcode.com”,“testemail+david@lee.tcode.com”]
Output: 2
Explanation: “testemail@leetcode.com” and “testemail@lee.tcode.com” actually receive mails.
Example 2:
Input: emails = [“a@leetcode.com”,“b@leetcode.com”,“c@leetcode.com”]
Output: 3
Constraints:
- 1 <= emails.length <= 100
- 1 <= emails[i].length <= 100
- emails[i] consist of lowercase English letters, ‘+’, ‘.’ and ‘@’.
- Each emails[i] contains exactly one ‘@’ character.
- All local and domain names are non-empty.
- Local names do not start with a ‘+’ character.
- Domain names end with the “.com” suffix.
- Domain names must contain at least one character before “.com” suffix.
From: LeetCode
Link: 929. Unique Email Addresses
Solution:
Ideas:
-
Input Parsing: We start by splitting the email string at ‘@’ to separate the local and domain names.
-
Local Name Transformation:
- We iterate through the local part and remove any ‘.’ characters.
- We stop processing the local name when we encounter the first ‘+’ sign.
-
Normalization: We then combine the processed local name and domain name to form the normalized email.
-
Uniqueness Check: We check if the email has already been added to the list of unique emails. If not, we add it to the list.
-
Result: Finally, we return the number of unique emails.
Code:
int numUniqueEmails(char** emails, int emailsSize) {
// To store unique email addresses
char **uniqueEmails = (char **)malloc(emailsSize * sizeof(char*));
int uniqueCount = 0;
for (int i = 0; i < emailsSize; i++) {
char *email = emails[i];
char *localName = strtok(email, "@"); // Get the local part
char *domainName = strtok(NULL, "@"); // Get the domain part
// Transform localName by removing '.' and ignoring characters after '+'
char *processedLocal = (char *)malloc(strlen(localName) + 1);
int j = 0;
for (int k = 0; k < strlen(localName); k++) {
if (localName[k] == '+') {
break; // Stop at the first '+' character
}
if (localName[k] != '.') {
processedLocal[j++] = localName[k]; // Keep non-'.' characters
}
}
processedLocal[j] = '\0'; // Null-terminate the string
// Construct the normalized email
char *normalizedEmail = (char *)malloc(strlen(processedLocal) + strlen(domainName) + 2);
sprintf(normalizedEmail, "%s@%s", processedLocal, domainName);
// Check if the normalized email is already in the set of unique emails
int isUnique = 1;
for (int k = 0; k < uniqueCount; k++) {
if (strcmp(uniqueEmails[k], normalizedEmail) == 0) {
isUnique = 0;
break;
}
}
// If unique, add it to the list
if (isUnique) {
uniqueEmails[uniqueCount++] = normalizedEmail;
} else {
free(normalizedEmail); // Free memory if not unique
}
free(processedLocal); // Free processed local name memory
}
// Return the number of unique emails
return uniqueCount;
}
更多推荐


所有评论(0)