当前位置:网站首页>Leetcode-929: unique email address
Leetcode-929: unique email address
2022-06-24 10:23:00 【Ugly and ugly】
Title Description
Each valid email address consists of a local name and a domain name , With ‘@’ symbols . Except lowercase letters , E-mail addresses can also contain one or more ‘.’ or ‘+’ .
for example , stay [email protected] in , alice Is a local name , and leetcode.com Is the domain name .
If you add a period between some characters in the local name part of the e-mail address (‘.’), Then the mail sent there will be forwarded to the same address without a dot in the local name . Please note that , This rule does not apply to domain names .
for example “[email protected]” and "[email protected]" Will be forwarded to the same email address .
If you add a plus sign to your local name (‘+’), Then everything after the first plus sign is ignored . This allows filtering of certain emails . Again , This rule Not applicable to domain name .
for example [email protected] Forward to [email protected]
You can use both rules at the same time .
Here's an array of strings emails, We'll tell everyone emails[i] Send an email . Return the number of different addresses that actually receive mail .
Example
Example 1:
Input :emails = [“[email protected]”, “[email protected]”, “[email protected]”]
Output :2
explain : The person who actually received the email was "[email protected]" and "[email protected]".
Example 2:
Input :emails = [“[email protected]”,“[email protected]”,“[email protected]”]
Output :3
The problem solving process
Ideas and steps
(1) Create temporary space Set To save a valid email address ;
(2) Traverse emails[] Array , With "@" Separate each email address into two parts , Some are local names , Part is domain name ;
(3) For local names with "+" Of , Do intercept operation , Ignore "+" What follows ;
(4) For local names and domain names "." Symbol , Do replace , take "." Replace with ""( An empty string ) that will do ;
(5) Put each email address in Set in , because Set Duplicate data is not allowed in , So in the end Set The amount of data in
Code display
public class NumUniqueEmails {
public int numUniqueEmails(String[] emails) {
Set<String> uniqueEmailsSet = new HashSet<>();
// With "@" The separated string is divided into two parts: local name and domain name , Only local names are processed
for (String email : emails) {
int j = email.lastIndexOf('@');
String localName = email.substring(0, j);
String domainName = email.substring(j);
if (localName.contains("+")) {
localName = localName.substring(0, localName.indexOf('+'));
}
localName = localName.replace(".", "");
uniqueEmailsSet.add(localName + domainName);
}
return uniqueEmailsSet.size();
}
public static void main(String[] args) {
String[] emails = {
"[email protected]", "[email protected]", "[email protected]"};
int result = new NumUniqueEmails().numUniqueEmails(emails);
System.out.println(result);
}
}
边栏推荐
- 被困英西中学的师生安全和食物有保障
- CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
- canvas无限扫描js特效代码
- leetCode-498: 对角线遍历
- Role of message queuing
- 机器学习——主成分分析(PCA)
- 学习使用phpstripslashe函数去除反斜杠
- Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5
- 记录一下MySql update会锁定哪些范围的数据
- PHP uses recursive and non recursive methods to create multi-level folders
猜你喜欢

2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)

机器学习——感知机及K近邻

4. classification management business development

411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)

H5网页如何在微信中自定义分享链接

5. dish management business development

How to manage massive network infrastructure?

Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5

Leetcode-498: diagonal traversal

百度网盘下载一直请求中问题解决
随机推荐
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
The great charm of cookies
How does home office manage the data center network infrastructure?
How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
学习使用php对字符串中的特殊符号进行过滤的方法
tf.contrib.layers.batch_norm
Floating point notation (summarized from cs61c and CMU CSAPP)
uniapp 开发微信公众号,下拉框默认选中列表第一个
4.分类管理业务开发
Leetcode-498: diagonal traversal
整理接口性能优化技巧,干掉慢代码
What are the characteristics of EDI local deployment and cloud hosting solutions?
时尚的弹出模态登录注册窗口
6.套餐管理业务开发
How to manage massive network infrastructure?
被困英西中学的师生安全和食物有保障
2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
MySQL data advanced