【leetcode】929. Unique Email Addresses

题目如下:

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

For example, in [email protected]alice 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, "[email protected]" and "[email protected]"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 [email protected] will be forwarded to [email protected].  (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: ["[email protected]","[email protected]","[email protected]"]
Output: 2
Explanation: "[email protected]" and "[email protected]" 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

时间: 2024-10-14 19:59:28

【leetcode】929. Unique Email Addresses的相关文章

929. Unique Email Addresses

Algorithm 做一个 leetcode 的算法题 Unique Email Addresses https://leetcode.com/problems/unique-email-addresses/ 1)problem 929. Unique Email Addresses Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email pro

[leetcode] 929. Unique Email Addresses (easy)

统计有几种邮箱地址. 邮箱名类型:[email protected] 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以及之后的local全部忽略. a+bc=a 思路: 利用set存,水题没啥好说的 Runtime: 20 ms, faster than 96.66% of C++ online submissions for Unique Email Addresses. class Solution { pub

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

Every email consists of a local name and a domain name, separated by the @ sign. For example, in?[email protected],?alice?is the local name, and?leetcode.com?is the domain name. Besides lowercase letters, these emails may contain?'.'s or?'+'s. If you

LeetCode 929 Unique Email Addresses 解题报告

题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. If you add periods ('.') between some characters in the local name 

【LeetCode】063. Unique Paths II

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the m

【LeetCode】96 - Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Tree Dynamic Programming Solution 1:  递归 1 class So

【LeetCode】93. Restore IP Addresses 【面试题】

Restore IP Addresses Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not mat

【leetcode】1207. Unique Number of Occurrences

题目如下: Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique. Example 1: Input: arr = [1,2,2,1,1,3] Output: true Explanation: The value 1 has 3 occurrences, 2 h

【LeetCode】96. Unique Binary Search Trees-唯一二叉排序树的个数

一.描述: 二.思路: BST(二叉排序树):中序遍历的结果为非递减序列,并且节点(个数和值)相同的不同二叉树的中序遍历结果都相同: 当左子树的节点个数确定后,右子树的个数也随之确定: 当节点个数为0或1时,二叉树只有1种,表示为f(0)=1,f(1)=f(0)*f(0): 当节点个数为2时,总的种类数=左子树为空f(0)*右子树不为空f(1)+左子树不为空f(1)*右子树为空f(0),即f(0)*f(1)+f(1)*f(0)=2种: 当节点个数为3时,有左子树为空f(0)*右子树不为空f(2)