LeetCode-929 Unique Email Addresses Solution (with Java)

1. Description:


Notes:

2. Examples:

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-02-24
 3  */
 4 class Solution {
 5         public int numUniqueEmails(String[] emails) {
 6         Set<String> normalized = new HashSet<>(); // used to save simplified email address, cost O(n) sapce.
 7         for (String email : emails) {
 8             String[] parts = email.split("@"); // split into local and domain parts.
 9             String[] local = parts[0].split("\\+"); // split local by '+'.
10             normalized.add(local[0].replace(".", "") + "@" + parts[1]); // remove all '.', and concatenate '@' and domain.        
11         }
12         return normalized.size();
13     }
14 }

 

posted @ 2020-03-02 15:33  SheepCore  阅读(142)  评论(0编辑  收藏  举报