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.
1function numUniqueEmails(emails) {
2 const uniqueEmails = new Set();
3 emails.forEach(email => {
4 const [local, domain] = email.split('@');
5 const processedLocal = local.split('+')[0].replace(/\./g, '');
6 uniqueEmails.add(`${processedLocal}@${domain}`);
7 });
8 return uniqueEmails.size;
9}
In JavaScript, use a Set to track unique emails. For each email, slice at '@' to extract local and domain components. Modify the local by removing '+' and beyond, and omit '.'. Combine with the domain and insert into the set. Unique email count is 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.
This is similar to Approach 1, but a slight variation in explanation to describe transformations as stepwise and using string-based mutation directly. The Python simplicity helps accomplish similar efficiency with identical methods.