首页 > 其他分享 >leetcode-929-easy

leetcode-929-easy

时间:2022-11-26 20:45:10浏览次数:40  
标签:name emails email 929 easy com leetcode sub

Unique Email Addresses

Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.

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 part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.

For example, "[email protected]" and "[email protected]" forward to the same email address.
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. Note that this rule does not apply to domain names.

For example, "[email protected]" will be forwarded to "[email protected]".
It is possible to use both of these rules at the same time.

Given an array of strings emails where we send one email to each emails[i], return the number of different addresses that actually receive mails.

Example 1:

Input: emails = ["[email protected]","[email protected]","[email protected]"]
Output: 2
Explanation: "[email protected]" and "[email protected]" actually receive mails.
Example 2:

Input: emails = ["[email protected]","[email protected]","[email protected]"]
Output: 3
Constraints:

1 <= emails.length <= 100
1 <= emails[i].length <= 100
emails[i] consist of lowercase English letters, '+', '.' and '@'.
Each emails[i] contains exactly one '@' character.
All local and domain names are non-empty.
Local names do not start with a '+' character.
Domain names end with the ".com" suffix.

思路一:取 @ 前面的字符串,先去除 + 后面的字符串,然后删除 . 字符,最后拼接字符即可

public int numUniqueEmails(String[] emails) {
    Set<String> set = new HashSet<>();

    for (String email : emails) {
        String sub = email.substring(0, email.indexOf('@'));
        int idx = sub.indexOf('+');
        if (idx >= 0) {
            sub = sub.substring(0, idx);
        }
        sub = sub.replace(".", "");

        set.add(sub + '@' + email.substring(email.indexOf('@')));
    }

    return set.size();
}

标签:name,emails,email,929,easy,com,leetcode,sub
From: https://www.cnblogs.com/iyiluo/p/16928251.html

相关文章

  • leetcode-696-easy
    CountBinarySubstringsGivenabinarystrings,returnthenumberofnon-emptysubstringsthathavethesamenumberof0'sand1's,andallthe0'sandallth......
  • leetcode-796-easy
    RotateStringGiventwostringssandgoal,returntrueifandonlyifscanbecomegoalaftersomenumberofshiftsons.Ashiftonsconsistsofmovingthe......
  • leetcode-1299-easy
    ReplaceElementswithGreatestElementonRightSideGivenanarrayarr,replaceeveryelementinthatarraywiththegreatestelementamongtheelementstoit......
  • leetcode-110-easy
    BalancedBinaryTreeGivenabinarytree,determineifitisheight-balanced.Example1:Input:root=[3,9,20,null,null,15,7]Output:trueExample2:Input......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:反转字符串中的单词 III
    题目:给定一个字符串 s ,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 示例1:输入:s="Let'stakeLeetCodecontest"输出:"s'teLekatedoCteeL......
  • easylogging++的那些事(四)源码分析(二)日志记录宏(一)CLOG宏(四)日志信息保存
    目录writer类的输出运算符writer类的流操控符el::base::MessageBuilder类CLOG宏接口调用流程图在上一篇中我们分析完了CLOG宏日志输出的流程,在结尾的时候我们提......
  • leetcode_D3_27移除元素
    1.题目   2.解一   主要思路:解一为本人解法,主要思路是先利用循环删除掉所有数组中值等于val的元素,然后可以直接返回数组的长度和其中的元素。感觉是没经过......
  • 二分查找-LeetCode704 简单题
    LeetCode代码链接:https://leetcode.cn/problems/binary-search/题目:给定一个 n 个元素有序的(升序)整型数组 nums和一个目标值 target ,写一个函数搜索 nums 中的t......
  • [LeetCode] 809. Expressive Words
    Sometimespeoplerepeatletterstorepresentextrafeeling.Forexample:"hello"->"heeellooo""hi"->"hiiii"Inthesestringslike "heeellooo",wehaveg......
  • leetcode 24. 两两交换链表中的节点 js实现
    给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。示例1:输入:head=[1,2,3,4]输出:[2,......