-
Notifications
You must be signed in to change notification settings - Fork 0
/
929. Unique Email Addresses.py
48 lines (35 loc) · 2.07 KB
/
929. Unique Email Addresses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
https://leetcode.com/problems/unique-email-addresses/
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 '+'.
For example, in "[email protected]", "alice" is the local name, and "leetcode.com" is the domain name.
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.
For example, "[email protected]" and "[email protected]" forward to the same email address.
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.
For example, "[email protected]" will be forwarded to "[email protected]".
It is possible to use both of these rules at the same time.
Given an array of strings emails where we send one email to each emails[i], return the number of different
addresses that actually receive mails.
Example 1:
Output: 2
Explanation: "[email protected]" and "[email protected]" actually receive mails.
Example 2:
Output: 3
"""
class Solution(object):
def numUniqueEmails(self, emails):
unique_emails = set()
for item in emails:
local_name, domain_name = item.split("@", 1)
local_name = local_name.split("+", 1)[0]
local_name = ''.join(local_name.split("."))
unique_emails.add(local_name + "@" + domain_name)
return len(unique_emails)
solution = Solution()
assert solution.numUniqueEmails(
assert solution.numUniqueEmails(["[email protected]", "[email protected]", "[email protected]"]) == 3