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.
1using System;
2using System.Collections.Generic;
3
4public class UniqueEmail
5{
6 public int NumUniqueEmails(string[] emails)
7 {
8 HashSet<string> uniqueEmails = new HashSet<string>();
9 foreach (string email in emails)
10 {
11 var parts = email.Split('@');
12 string local = parts[0];
13 string domain = parts[1];
14 int plusIndex = local.IndexOf('+');
15 if (plusIndex != -1)
16 {
17 local = local.Substring(0, plusIndex);
18 }
19 local = local.Replace(".", "");
20 uniqueEmails.Add(local + "@" + domain);
21 }
22 return uniqueEmails.Count;
23 }
24}
In C#, employ a HashSet to store unique emails. For each email, partition by '@', manage the local part by halting at '+' and eradicating '.'. Combine with the domain thereafter, and populate the set with the final email. Return the count via the set's size.
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.
#include <vector>
#include <string>
#include <unordered_set>
using namespace std;
int numUniqueEmails(vector<string>& emails) {
unordered_set<string> seenEmails;
for (auto& email : emails) {
string local, domain;
bool at = false, plus = false;
for (char c : email) {
if (c == '@') {
at = true;
if (!plus) {
seenEmails.insert(local + domain);
}
}
if (at) {
domain += c;
} else {
if (c == '+'){
plus = true;
} else if (c != '.' && !plus) {
local += c;
}
}
}
}
return seenEmails.size();
}
This code avoids splitting by '@' to showcase opposite direct changes and insertion to component variables as handling occurs. Rather than invoking standard libraries, it uses explicit logic to chain append operations on local and domain segments.