当前位置:网站首页>Leetcode-929: unique email address

Leetcode-929: unique email address

2022-06-24 10:23:00 Ugly and ugly

Title Description

Each valid email address consists of a local name and a domain name , With ‘@’ symbols . Except lowercase letters , E-mail addresses can also contain one or more ‘.’ or ‘+’ .
for example , stay [email protected] in , alice Is a local name , and leetcode.com Is the domain name .
If you add a period between some characters in the local name part of the e-mail address (‘.’), Then the mail sent there will be forwarded to the same address without a dot in the local name . Please note that , This rule does not apply to domain names .
for example “[email protected]” and "[email protected]" Will be forwarded to the same email address .
If you add a plus sign to your local name (‘+’), Then everything after the first plus sign is ignored . This allows filtering of certain emails . Again , This rule Not applicable to domain name .
for example [email protected] Forward to [email protected]
You can use both rules at the same time .
Here's an array of strings emails, We'll tell everyone emails[i] Send an email . Return the number of different addresses that actually receive mail .

Example

Example 1:
Input :emails = [“[email protected]”, “[email protected]”, “[email protected]”]
Output :2
explain : The person who actually received the email was "[email protected]" and "[email protected]".

Example 2:
Input :emails = [“[email protected]”,“[email protected]”,“[email protected]”]
Output :3

The problem solving process

Ideas and steps

(1) Create temporary space  Set  To save a valid email address ;
(2) Traverse  emails[]  Array , With  "@"  Separate each email address into two parts , Some are local names , Part is domain name ;
(3) For local names with  "+"  Of , Do intercept operation , Ignore  "+"  What follows ;
(4) For local names and domain names  "."  Symbol , Do replace , take  "."  Replace with  ""( An empty string ) that will do ;
(5) Put each email address in  Set  in , because  Set  Duplicate data is not allowed in , So in the end  Set  The amount of data in 

Code display

public class NumUniqueEmails {
    

    public int numUniqueEmails(String[] emails) {
    
        Set<String> uniqueEmailsSet = new HashSet<>();
        //  With  "@"  The separated string is divided into two parts: local name and domain name ,  Only local names are processed 
        for (String email : emails) {
    
            int j = email.lastIndexOf('@');
            String localName = email.substring(0, j);
            String domainName = email.substring(j);
            if (localName.contains("+")) {
    
                localName = localName.substring(0, localName.indexOf('+'));
            }
            localName = localName.replace(".", "");
            uniqueEmailsSet.add(localName + domainName);
        }
        return uniqueEmailsSet.size();
    }

    public static void main(String[] args) {
    
        String[] emails = {
    "[email protected]", "[email protected]", "[email protected]"};
        int result = new NumUniqueEmails().numUniqueEmails(emails);
        System.out.println(result);
    }

}
原网站

版权声明
本文为[Ugly and ugly]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240916245556.html