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.
1def num_unique_emails(emails):
2 unique_emails = set()
3 for email in emails:
4 local, domain = email.split('@')
5 local = local.split('+')[0].replace('.', '')
6 unique_emails.add(f"{local}@{domain}")
7 return len(unique_emails)
8
In Python, we use a set to hold unique email addresses. We split each email by '@' to get local and domain parts. The local part is then split at the '+' to ignore everything beyond it and replaced '.' with ''. The processed local part is then combined with the domain part before adding to the set. The number of unique emails is the size of the set.
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.