Unique Email Groups Problem
Description
LeetCode Problem 3860.
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:
- The local name is the part before the
'@'symbol.- Ignore all dots
'.'. - Ignore everything after the first
'+', if present. - Convert to lowercase.
- Ignore all dots
- The domain name is the part after the
'@'symbol.- Convert to lowercase.
Return an integer denoting the number of unique email groups after normalization.
Example 1:
Input: emails = ["[email protected]", "[email protected]", "[email protected]"]
Output: 2
Explanation:
| Local | Normalized Local | Domain | Normalized Domain | Final Email | |
|---|---|---|---|---|---|
| [email protected] | test.email+alex | testemail | leetcode.com | leetcode.com | [email protected] |
| [email protected] | test.e.mail+bob.cathy | testemail | leetcode.com | leetcode.com | [email protected] |
| [email protected] | testemail+david | testemail | lee.tcode.com | lee.tcode.com | [email protected] |
Unique emails are ["[email protected]", "[email protected]"]. Thus, the answer is 2.
Example 2:
Input: emails = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
Output: 2
Explanation:
Email Local Normalized Local Domain Normalized Domain Final Email
[email protected] A a B.com b.com [email protected]
[email protected] a a b.com b.com [email protected]
[email protected] ab+xy ab b.com b.com [email protected]
[email protected] a.b ab b.com b.com [email protected]
Unique emails are ["[email protected]", "[email protected]"]. Thus, the answer is 2.
Example 3:
Input: emails = ["[email protected]", "[email protected]", "[email protected]"]
Output: 1
Explanation:
Email Local Normalized Local Domain Normalized Domain Final Email
[email protected] a.b+c.d+e ab DoMain.com domain.com [email protected]
[email protected] ab+xyz ab domain.com domain.com [email protected]
[email protected] ab ab domain.com domain.com [email protected]
All emails normalize to "[email protected]". 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'@'.- Each
emails[i]contains exactly one'@'character. - All local and domain names are non-empty; local names do not start with
'+'. - Domain names end with the
".com"suffix and contain at least one character before".com".
Solution
Approach: Normalize and Count in a Hash Set
Normalize every address, then count the distinct results. For the local name, cut it at the first +, drop all dots and lowercase it. Lowercase the domain. Join them with @ and put the result in a hash set; the set size is the number of groups.
Complexity: O(n * m) time and space, where n is the number of emails and m their average length.
class Solution:
def uniqueEmailGroups(self, emails: List[str]) -> int:
st = set()
for email in emails:
local, domain = email.split("@")
local = local.split("+")[0].replace(".", "").lower()
domain = domain.lower()
normalized = local + "@" + domain
st.add(normalized)
return len(st)