You are given an array of strings emails, where each string is a valid email address.
Two email addresses belong to the same group if both their normalized local names and normalized domain names are identical.
The normalization rules are as follows:
'@' symbol.
'.'.'+', if present.'@' symbol.
Return an integer denoting the number of unique email groups after normalization.
Example 1:
Input: emails = ["test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com", "testemail+david@lee.tcode.com"]
Output: 2
Explanation:
| Local | Normalized Local | Domain | Normalized Domain | Final Email | |
|---|---|---|---|---|---|
| test.email+alex@leetcode.com | test.email+alex | testemail | leetcode.com | leetcode.com | testemail@leetcode.com |
| test.e.mail+bob.cathy@leetcode.com | test.e.mail+bob.cathy | testemail | leetcode.com | leetcode.com | testemail@leetcode.com |
| testemail+david@lee.tcode.com | testemail+david | testemail | lee.tcode.com | lee.tcode.com | testemail@lee.tcode.com |
Unique emails are ["testemail@leetcode.com", "testemail@lee.tcode.com"]. Thus, the answer is 2.
Example 2:
Input: emails = ["A@B.com", "a@b.com", "ab+xy@b.com", "a.b@b.com"]
Output: 2
Explanation:
| Local | Normalized Local | Domain | Normalized Domain | Final Email | |
|---|---|---|---|---|---|
| A@B.com | A | a | B.com | b.com | a@b.com |
| a@b.com | a | a | b.com | b.com | a@b.com |
| ab+xy@b.com | ab+xy | ab | b.com | b.com | ab@b.com |
| a.b@b.com | a.b | ab | b.com | b.com | ab@b.com |
Unique emails are ["a@b.com", "ab@b.com"]. Thus, the answer is 2.
Example 3:
Input: emails = ["a.b+c.d+e@DoMain.com", "ab+xyz@domain.com", "ab@domain.com"]
Output: 1
Explanation:
| Local | Normalized Local | Domain | Normalized Domain | Final Email | |
|---|---|---|---|---|---|
| a.b+c.d+e@DoMain.com | a.b+c.d+e | ab | DoMain.com | domain.com | ab@domain.com |
| ab+xyz@domain.com | ab+xyz | ab | domain.com | domain.com | ab@domain.com |
| ab@domain.com | ab | ab | domain.com | domain.com | ab@domain.com |
All emails normalize to "ab@domain.com". Thus, the answer is 1.
Constraints:
1 <= emails.length <= 10001 <= emails[i].length <= 100emails[i] consists of lowercase and uppercase English letters, digits, and the characters '.', '+', and '@'.emails[i] contains exactly one '@' character.'+'.".com" suffix and contain at least one character before ".com".We can use a hash set st to store the normalized result of each email address. For each email address, we normalize it according to the problem requirements:
., and if a plus sign + exists, remove the plus sign and everything after it. Then convert the local name to lowercase.st.Finally, the number of elements in the hash set st is the number of unique email groups.
The time complexity is O(n cdot m), where n and m are the number of email addresses and the average length of each email address, respectively. The space complexity is O(n cdot m), in the worst case where all email addresses are distinct.
Java
C++
Go
TypeScript
Practice Unique Email Groups with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor