Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd81173

Browse files
author
weiy
committed
unique email address easy
1 parent d651201 commit bd81173

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

‎String/UniqueEmailAddresses.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Every email consists of a local name and a domain name, separated by the @ sign.
3+
4+
For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.
5+
6+
Besides lowercase letters, these emails may contain '.'s or '+'s.
7+
8+
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. For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address. (Note that this rule does not apply for domain names.)
9+
10+
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, for example m.y+name@email.com will be forwarded to my@email.com. (Again, this rule does not apply for domain names.)
11+
12+
It is possible to use both of these rules at the same time.
13+
14+
Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?
15+
16+
17+
18+
Example 1:
19+
20+
Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
21+
Output: 2
22+
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails
23+
24+
25+
Note:
26+
27+
1 <= emails[i].length <= 100
28+
1 <= emails.length <= 100
29+
Each emails[i] contains exactly one '@' character.
30+
31+
32+
替换 . 和 + 即可。
33+
34+
测试地址:
35+
https://leetcode.com/contest/weekly-contest-108/problems/unique-email-addresses/
36+
37+
"""
38+
import re
39+
40+
class Solution(object):
41+
def numUniqueEmails(self, emails):
42+
"""
43+
:type emails: List[str]
44+
:rtype: int
45+
"""
46+
47+
result = 0
48+
# local = {}
49+
_emails = set()
50+
ignore = re.compile(r'\+(.*)')
51+
for i in emails:
52+
x = i.split('@')
53+
if len(x) > 2:
54+
continue
55+
56+
if len(x) == 1:
57+
continue
58+
59+
local = x[0]
60+
domain = x[1]
61+
local = local.replace('.', '')
62+
63+
64+
local = re.sub(ignore, '', local)
65+
66+
_emails.add(local + '@' + domain)
67+
68+
return len(_emails)
69+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /