congr / world

2 stars 1 forks source link

LeetCode : 929. Unique Email Addresses #435

Closed congr closed 5 years ago

congr commented 5 years ago

https://leetcode.com/problems/unique-email-addresses/

image

Example 1:

Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails
congr commented 5 years ago
class Solution {
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet();

        for (String s : emails) {
            int at = s.indexOf("@");
            String local = s.substring(0, at);
            String domain = s.substring(at, s.length());

            String[] r = local.split("/+"); // !!!
            String t = r[0].replaceAll(".", "");
            set.add (t + domain);         
        }

        return set.size();
    }
}