Each email is composed of a local name and a domain name, which are separated by the @ symbol.
For example[email protected]
Medium,alice
Is the local name, whileleetcode.com
Yes.
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
- Each
emails[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