LeetCode - 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

Hash

class Solution {
    public int numUniqueEmails(String[] emails) {
        if(emails == null || emails.length == 0){
            return 0;
        }
        Set<String> set = new HashSet<>();
        for(String email : emails){
            String[] strs = email.split("@");
            String localName = strs[0];
            String domainName = strs[1];
            StringBuilder sb = new StringBuilder();
            for(char c : localName.toCharArray()){
                if(c == ‘.‘){
                    continue;
                }
                else if(c == ‘+‘){
                    break;
                }
                else{
                    sb.append(c);
                }
            }
            set.add(sb.toString() + "@" + domainName);
        }
        return set.size();
    }
}

原文地址:https://www.cnblogs.com/incrediblechangshuo/p/9977047.html

时间: 2024-10-12 15:08:04

LeetCode - Unique Email Addresses的相关文章

[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

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

题目如下: 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.

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] 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

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | 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(独特的电子邮件地址)

1 class Solution 2 { 3 public: 4 int numUniqueEmails(vector<string>& emails) 5 { 6 for(int i = 0;i < emails.size();i ++) 7 { 8 for(int j = 0;j < emails[i].size();j ++) 9 { 10 if(emails[i][j]=='@') 11 { 12 for(int k = 0;k < j;k ++) 13 {

[LeetCode] Unique Binary Search Trees II (难以忍受的递归)

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 class Solution { private

[leetcode]Unique Binary Search Trees II @ Python

原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考虑dfs了.dfs(start, end)函数返回以start,start+1,...,end为根的二叉查找树. 代码: # Definition for a binary tree node #