-
Notifications
You must be signed in to change notification settings - Fork 29
/
0929-Unique-Email-Addresses.py
70 lines (55 loc) · 2.8 KB
/
0929-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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'''
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
Constraints:
1 <= emails.length <= 100
1 <= emails[i].length <= 100
emails[i] consist of lowercase English letters, '+', '.' and '@'.
Each emails[i] contains exactly one '@' character.
All local and domain names are non-empty.
Local names do not start with a '+' character.
Domain names end with the ".com" suffix.
'''
# Normal Iteration
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
res = set()
for email in emails:
new_email = []
for char in email:
if char == '+' or char == '@':
break
if char != '.':
new_email.append(char)
domain_name = []
for char in reversed(email):
domain_name.append(char)
if char == '@':
break
new_email = ''.join(new_email)
domain_name = ''.join(domain_name[::-1])
res.add(new_email+domain_name)
return len(res)
# Using in-built Function
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
res = set()
for email in emails:
name, domain_name = email.split('@')
new_email = name.split('+')
new_email = new_email[0].replace('.', '')
res.add(new_email+'@'+domain_name)
return len(res)