Personal questions about the unique email address of the leetcode

Source: Internet
Author: User
Tags to domain

Each email is composed of a local name and a domain name, which are separated by the @ symbol.

For example[email protected]Medium,aliceIs the local name, whileleetcode.comYes.

In addition to lowercase letters, these emails may also contain‘,‘Or‘+‘.

If you add a period (‘.‘), The email to be sent will be forwarded to the same address without any point in the local name. For example,"[email protected]”And“[email protected]”Will be forwarded to the same email address. (Please note that this rule does not apply to domain names .)

If you add the plus sign (‘+‘Is ignored. This allows filtering of certain emails, such[email protected]Forward[email protected]. (Similarly, this rule does not apply to domain names .)

These two rules can be used at the same time.

Given email listemails, We will send an email to each address in the list. What are the actual addresses of emails?

 

Example:

Input: ["[email protected]", "[email protected]", "[email protected]"] Output: 2 explanation: the actual emails received are "[email protected]" and "[email protected]".

 

Tip:

  • 1 <= emails[i].length <= 100
  • 1 <= emails.length <= 100
  • Eachemails[i]All include and only one‘@‘Character.

 

This question is very simple. Java encapsulates the string class well and uses several built-in functions to solve the problem. To separate the local name from the domain name, then operate the local name string, replace "." with "", and remove all the strings after "+.

However, we still encountered problems. In Java of higher versions, you can use list. The compiler in leetcode only supports arraylist.

The Code is as follows:

 1 class Solution { 2  3     public int numUniqueEmails(String[] emails) { 4         ArrayList<String> ans = new ArrayList<>(); 5         for (String s : emails) { 6             String tmp[] = s.split("@"); 7             tmp[0] = tmp[0].replace(".", ""); 8             for (int i = 0; i < tmp[0].length(); i++) { 9                 if (tmp[0].charAt(i) == ‘+‘) {10                     tmp[0] = tmp[0].substring(0, i);11                 }12             }13             s = tmp[0] + "@" + tmp[1];14             if (ans.size() == 0)15                 ans.add(s);16             for (int j = 0; j < ans.size(); j++) {17                 if (s.equals(ans.get(j))) {18                     break;19                 }20                 if (j == ans.size() - 1)21                     ans.add(s);22             }23 24         }25         return ans.size();26     }27 }

 

Personal questions about the unique email address of the leetcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.