Every email consists of a local name and a domain name, separated by the @ sign.

For example, in alice@leetcode.comalice is the local name, and leetcode.com is the domain name.

Besides lowercase letters, these emails may contain '.'s or '+'s.

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.)

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.)

It is possible to use both of these rules at the same time.

Given a list of emails, we send one email to each address in the list.  How many different addresses actually receive mails?

Example 1:

Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails

Note:

  • 1 <= emails[i].length <= 100
  • 1 <= emails.length <= 100
  • Each emails[i] contains exactly one '@' character.
  • All local and domain names are non-empty.
  • Local names do not start with a '+' character.
 解法:根据题目要求,直接在@处分解email,把local name中的'.'和'+'之后的字符去掉,在和domain拼接成email地址,然后存进set,set可以去重,最后返回set的长度。
Java:
public int numUniqueEmails(String[] emails) {
Set<String> normalized = new HashSet<>(); // used to save simplified email address, cost O(n) sapce.
for (String email : emails) {
String[] parts = email.split("@"); // split into local and domain parts.
String[] local = parts[0].split("\\+"); // split local by '+'.
normalized.add(local[0].replace(".", "") + "@" + parts[1]); // remove all '.', and concatenate '@' and domain.
}
return normalized.size();
}  

Python:

class Solution:
def numUniqueEmails(self, emails):
"""
:type emails: List[str]
:rtype: int
"""
email_set = set()
for email in emails:
local_name,domain_name = email.split("@")
local_name ="".join(local_name.split('+')[0].split('.'))
email = local_name +'@' + domain_name
email_set.add(email)
return len(email_set)

Python: wo

class Solution(object):
def numUniqueEmails(self, emails):
"""
:type emails: List[str]
:rtype: int
"""
ans = set()
for email in emails:
split_names = email.split('@')
local_name = split_names[0]
domain_name = split_names[1]
local_name_remove_plus = local_name.split('+')[0]
local_name_split_dot = local_name_remove_plus.split('.')
local_name = ('').join(local_name_split_dot)
new_email = local_name + '@' + domain_name
ans.add(new_email)
return len(ans)

C++:

class Solution
{
public: using Emails = vector< string >;
using Unique = unordered_set< string >; int numUniqueEmails( Emails& emails, Unique unique={} )
{
for( auto& e: emails )
{
auto pivot = e.find_first_of( '@' );
auto name = e.substr( 0, pivot ),
domain = e.substr( pivot );
name.erase( remove( name.begin(), name.end(), '.' ), name.end() );
auto pos = name.find_first_of( '+' );
unique.insert( ( pos != string::npos )? name.erase( pos ) + domain : name + domain );
}
return static_cast< int >( unique.size() );
} };

  

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 929. Unique Email Addresses 唯一的电邮地址的更多相关文章

  1. [leetcode] 929. Unique Email Addresses (easy)

    统计有几种邮箱地址. 邮箱名类型:local@domain 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以 ...

  2. [LeetCode] 929. Unique Email Addresses 独特的邮件地址

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  3. LeetCode 929.Unique Email Addresses

    Description Every email consists of a local name and a domain name, separated by the @ sign. For exa ...

  4. LeetCode 929 Unique Email Addresses 解题报告

    题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, i ...

  5. LeetCode 929. Unique Email Addresses (独特的电子邮件地址)

    题目标签:String 题目说明 有两个规则针对于 local name. 所以先把local name 和 domain name 分开. 两个规则是: rule 1:'.' 会被去除. (利用re ...

  6. 929. Unique Email Addresses

    929. Unique Email Addresses Easy 22766FavoriteShare Every email consists of a local name and a domai ...

  7. 【Leetcode_easy】929. Unique Email Addresses

    problem 929. Unique Email Addresses solution: class Solution { public: int numUniqueEmails(vector< ...

  8. 【leetcode】929. Unique Email Addresses

    题目如下: Every email consists of a local name and a domain name, separated by the @ sign. For example, ...

  9. 【LeetCode】929. Unique Email Addresses 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set + 字符串操作 参考资料 日期 题目地址:h ...

随机推荐

  1. 关于Certificate、Provisioning Profile

    Certificate(证书)就是app在打包的时候必须签名,苹果iOS系统在安装app之前会验证Certificate,否则不会通过安装. Provisioning Profile简单来说就是包含A ...

  2. 17、Python面向对象高级

    一.isinstance和issubclass type():不会认为子类实例是一种父类类型: isinstance():认为子类实例是一种父类类型. issubclass():判断是否为其子类. c ...

  3. 前端jsp fetch跨域调用 is not allowed by Access-Control-Allow-Origin.

    之前我在用json跨域调用时,遇到如图问题,后来查查是官方json不支持跨域调用,后来改用非官方的jsonp跨域调用后台方法,出现如下问题 Origin http://127.0.0.1:8080 i ...

  4. 使用Map文件查找崩溃信息

    简介 编写整洁的应用程序是一回事.但是当用户告诉你你的软件已经崩溃时,你知道在添加其他功能之前最好先解决这个问题.如果你够幸运的话,用户会有一个崩溃地址.这将大大有助于解决这个问题.但是你怎么能用这个 ...

  5. [转]Reids配置文件redis.conf中文详解

    转自: Reids配置文件redis.conf中文详解 redis的各种配置都是在redis.conf文件中进行配置的. 有关其每项配置的中文详细解释如下: 对应的中文版解释redis.conf # ...

  6. [Android] Android studio gradle 插件的版本号和 gradle 的版本号 的对应关系

    [Android] Android studio gradle 插件的版本号和 gradle 的版本号 的对应关系 本博客地址: wukong1688 本文原文地址:https://www.cnblo ...

  7. 如何高效的阅读uni-app框架?(建议收藏)

    作者 | Jeskson来源 | 达达前端小酒馆 uni-app的框架,配置:page.json,manifest.json,package.json,vue.config.js.脚本,应用程序,ma ...

  8. 一篇JavaScript技术栈带你了解继承和原型链

    作者 | Jeskson 来源 | 达达前端小酒馆 1 在学习JavaScript中,我们知道它是一种灵活的语言,具有面向对象,函数式风格的编程模式,面向对象具有两点要记住,三大特性,六大原则. 那么 ...

  9. 一个bug程序员的入园

    大家好,我叫dg是一个只写bug的程序员.当然只写bug也是有好处的,那就是踩过的坑多了,摔的跟斗多了,并且没有被摔死,勇敢的活了下来,练就了一身钢筋铁骨.哈哈,开个玩笑.但是猜的坑多了就知道了哪里有 ...

  10. 回滚事件只是让原数据看起来不变,但是id还是会自增吗?

    回滚事件只是让原数据看起来不变,但是id还是会自增对吗? 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 ...