当前位置:网站首页>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);
}
}
边栏推荐
- 微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
- leetCode-2221: 数组的三角和
- Wechat applet learning to achieve list rendering and conditional rendering
- Go language development environment setup +goland configuration under the latest Windows
- MYSQL数据高级
- Leetcode-1089: replication zero
- 2.登陆退出功能开发
- What are the characteristics of EDI local deployment and cloud hosting solutions?
- 6.套餐管理业务开发
- Getting user information for applet learning (getuserprofile and getUserInfo)
猜你喜欢

3.员工的增删改查

线程池的执行流程

Uniapp implements the function of clicking to make a call

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

美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架

百度网盘下载一直请求中问题解决

JMeter接口测试工具基础— 取样器sampler(二)

Record the range of data that MySQL update will lock

利用pandas读取SQL Sever数据表

SQL Sever关于like操作符(包括字段数据自动填充空格问题)
随机推荐
The great charm of cookies
Status of the thread pool
Normal equation
Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
消息队列的作用
4. classification management business development
牛客-TOP101-BM28
小程序 rich-text中图片点击放大与自适应大小问题
leetCode-929: 独特的电子邮件地址
Resolved: methods with the same name as their class will not be constructors in
p5.js千纸鹤动画背景js特效
Getting user information for applet learning (getuserprofile and getUserInfo)
413-二叉树基础
图解杂项【防止丢失进行存档用的】
Why is JSX syntax so popular?
1.项目环境搭建
leetCode-1823: 找出游戏的获胜者
植物生长h5动画js特效
tf.errors
tf.contrib.layers.batch_norm