
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 '+'.
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.
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.
It is possible to use both of these rules at the same time.
Given an array of 'N' strings 'emails' where we send one email to each emails[i], return the number of different addresses that actually receive mails.
Let 'emails' = ["test.email+alex@ninjas.com", "test.e.mail+bob.case@ninjas.com", "testemail@ninjas.com"].
We need to find the number of unique email addresses after applying the rules.
The first email "test.email+alex@ninjas.com":
The local name is "test.email+alex" and the domain name is "ninjas.com".
Applying the rules to the local name:
Remove '.' -> "testemail+alex"
Ignore everything after '+' -> "testemail"
The processed email address is "testemail@ninjas.com".
The second email "test.e.mail+bob.case@ninjas.com":
The local name is "test.e.mail+bob.case" and the domain name is "ninjas.com".
Applying the rules to the local name:
Remove '.' -> "testemail+bob.case"
Ignore everything after '+' -> "testemail"
The processed email address is "testemail@ninjas.com".
The third email "testemail@ninjas.com":
The local name is "testemail" and the domain name is "ninjas.com".
No '.' or '+' in the local name.
The processed email address is "testemail@ninjas.com".
The unique processed email addresses are {"testemail@ninjas.com"}.
Therefore, the number of different addresses that actually receive mails is 1.
The first line contains a single integer 'N' denoting the length of the array.
The second line contains the array of strings 'emails'.
Output Format :
Return a single integer, the number of different addresses that actually receive mails.
Note :
You don’t need to print anything. Just implement the given function.
1 <= 'N' <= 100
1 <= length of emails[i] <= 100
emails[i] consists of lowercase letters, '.', '+', and '@'.
Each emails[i] contains exactly one '@' character.
The local and domain names are non-empty.
Time Limit: 1 sec
3
a@ninjas.com b@ninjas.com c@ninjas.com
3
The given emails are ["a@ninjas.com","b@ninjas.com","c@ninjas.com"].
The first email "a@ninjas.com": local name "a", domain name "ninjas.com". Processed: "a@ninjas.com".
The second email "b@ninjas.com": local name "b", domain name "ninjas.com". Processed: "b@ninjas.com".
The third email "c@ninjas.com": local name "c", domain name "ninjas.com". Processed: "c@ninjas.com".
All three processed emails are unique: {"a@ninjas.com", "b@ninjas.com", "c@ninjas.com"}.
Thus, the answer is 3.
3
test.email+alex@ninjas.com test.email.goo+ciao@ninjas.com testemail@ninjas.com
2
How can you efficiently keep track of the unique email addresses after processing?
Approach:
Algorithm:
O(N * M), where 'N' is the number of emails in the input array 'emails' and 'M' is the maximum length of an email string.
We iterate through each of the 'N' email strings. For each email, we iterate through its characters up to the '@' symbol to process the local name. In the worst case, processing an email of length 'M' takes O(M) time. Adding to a set on average takes time proportional to the length of the string being added, which is at most O(M). Thus, the overall time complexity is of the order O(N * M).
O(N * M), where 'N' is the number of emails in the input array 'emails' and 'M' is the maximum length of an email string.
In the worst case, all processed email addresses are unique and are stored in the set. The set can store up to 'N' strings, and each processed string can have a length up to 'M' (in the case where the local name contains no '.' or '+'). Thus, the overall space complexity is of the order O(N * M).