博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】929. Unique Email Addresses
阅读量:7200 次
发布时间:2019-06-29

本文共 1914 字,大约阅读时间需要 6 分钟。

题目如下:

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: 2Explanation: "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.

解题思路:82%的通过率足以证明这题有多简单。把local name的 '.'替换成'',并且截断第一个'+'后面的内容即可。

代码如下:

class Solution(object):    def numUniqueEmails(self, emails):        """        :type emails: List[str]        :rtype: int        """        dic = {}        for i in emails:            e = i.split('@')            e[0] = e[0].replace('.','')            if '+' in e[0]:                e[0] = e[0][:e[0].index('+')]            if e[0] + '@' +  e[1] not in dic:                dic[e[0] + '@' +  e[1]] = 1        #print dic        return len(dic)

 

转载于:https://www.cnblogs.com/seyjs/p/9865597.html

你可能感兴趣的文章
读书笔记2013第9本:《注意力曲线----打败分心与焦虑》
查看>>
为TWaver HTML5的Tree组件增加鼠标滑过效果
查看>>
怎样收集有用的QQ号码为我所用
查看>>
hive-极致优化(二)-解释计划类别
查看>>
ajax跨域
查看>>
webpack4配置详解之常用插件分享
查看>>
phalcon7 阅读理解
查看>>
消息中间件 RocketMQ源码解析:Message顺序发送与消费
查看>>
Thread interrupt
查看>>
博客搬家至简书
查看>>
Linux上的free命令详解
查看>>
linux主目录下各个子目录的作用
查看>>
[问题]javax.servlet不存在的问题
查看>>
Hive学习总结之五:HBase和Hive的集成
查看>>
Windows7系统中启动Windows Event Log服务提示拒绝访问,错误5的解决方法
查看>>
mybatis--缓存(一级和二级缓存)
查看>>
centos 配置 nginx + fcgiwrap + git
查看>>
Eclipse下Maven打包非法字符问题
查看>>
FreeMarker标签
查看>>
AngularJS 中的 Promise 和 设计模式
查看>>