Skip to content

Commit 7f5ff13

Browse files
committed
🎉 #929. Unique Email Addresses
1 parent f0a9d53 commit 7f5ff13

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: Goog Tech
3+
* @Date: 2020-08-29 23:17:50
4+
* @LastEditTime: 2020-08-29 23:18:19
5+
* @Description: https://leetcode-cn.com/problems/unique-email-addresses/
6+
* @FilePath: \leetcode-googtech\#929. Unique Email Addresses\Solution.java
7+
* @WebSite: https://algorithm.show/
8+
*/
9+
10+
class Solution {
11+
public int numUniqueEmails(String[] emails) {
12+
Set<String> set = new HashSet<>();
13+
for(String email : emails) {
14+
StringBuilder sb = new StringBuilder();
15+
// substring(int beginIndex, int endIndex)
16+
sb.append(email.substring(0, email.indexOf('+') == -1 ? email.indexOf('@') : email.indexOf('+')).replace(".", ""));
17+
// substring(int beginIndex)
18+
sb.append(email.substring(email.indexOf('@')));
19+
set.add(sb.toString());
20+
}
21+
return set.size();
22+
}
23+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Author: Goog Tech
3+
Date: 2020-08-29 23:17:56
4+
LastEditTime: 2020-08-29 23:18:12
5+
Description: https://leetcode-cn.com/problems/unique-email-addresses/
6+
FilePath: \leetcode-googtech\#929. Unique Email Addresses\Solution.py
7+
WebSite: https://algorithm.show/
8+
'''
9+
10+
class Solution(object):
11+
def numUniqueEmails(self, emails):
12+
"""
13+
:type emails: List[str]
14+
:rtype: int
15+
"""
16+
# 初始化用于存储邮件地址的 Set 集合
17+
emailSet = set()
18+
# 逐个遍历数组中的邮件地址
19+
for email in emails:
20+
# 分割邮件中的本地名称与域名
21+
name, domain = email.split('@')
22+
# 判断本地名称中是否存在符号 '+'
23+
if name.find('+') == -1:
24+
# 本地名称中不存在符号 '+',所以仅删除名称中的符号 '.' 即可
25+
name = name.replace('.','')
26+
else:
27+
# 忽略本地名称中符号 '+' 后面的字符,并删除名称中的符号 '.'
28+
name = name[:name.find('+')].replace('.', '')
29+
# 重新组合本地名称与域名,并将其存储到 Set 集合中
30+
emailSet.add(name + '@' + domain)
31+
# 返回 Set 集合中不同邮件的数量
32+
return len(emailSet)

0 commit comments

Comments
 (0)