Welcome to Subscribe On Youtube

929. Unique Email Addresses

Description

Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.

  • For example, in "alice@leetcode.com", "alice" is the local name, and "leetcode.com" is the domain name.

If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.

  • For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.

If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.

  • For example, "m.y+name@email.com" will be forwarded to "my@email.com".

It is possible to use both of these rules at the same time.

Given an array of strings emails where we send one email to each emails[i], return the number of different addresses that actually receive mails.

 

Example 1:

Input: emails = ["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.

Example 2:

Input: emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
Output: 3

 

Constraints:

  • 1 <= emails.length <= 100
  • 1 <= emails[i].length <= 100
  • emails[i] consist of lowercase English letters, '+', '.' and '@'.
  • Each emails[i] contains exactly one '@' character.
  • All local and domain names are non-empty.
  • Local names do not start with a '+' character.
  • Domain names end with the ".com" suffix.

Solutions

  • class Solution {
        public int numUniqueEmails(String[] emails) {
            Set<String> s = new HashSet<>();
            for (String email : emails) {
                String[] t = email.split("@");
                String local = t[0].replace(".", "");
                String domain = t[1];
                int i = local.indexOf('+');
                if (i != -1) {
                    local = local.substring(0, i);
                }
                s.add(local + "@" + domain);
            }
            return s.size();
        }
    }
    
  • class Solution {
    public:
        int numUniqueEmails(vector<string>& emails) {
            unordered_set<string> s;
            for (auto& email : emails) {
                int i = email.find('@');
                string local = email.substr(0, i);
                string domain = email.substr(i + 1);
                i = local.find('+', 0);
                if (~i) local = local.substr(0, i);
                while (~(i = local.find('.', 0)))
                    local.erase(local.begin() + i);
                s.insert(local + "@" + domain);
            }
            return s.size();
        }
    };
    
  • class Solution:
        def numUniqueEmails(self, emails: List[str]) -> int:
            s = set()
            for email in emails:
                local, domain = email.split('@')
                local = local.replace('.', '')
                if (i := local.find('+')) != -1:
                    local = local[:i]
                s.add(local + '@' + domain)
            return len(s)
    
    
  • func numUniqueEmails(emails []string) int {
    	s := map[string]bool{}
    	for _, email := range emails {
    		i := strings.IndexByte(email, '@')
    		local := strings.SplitN(email[:i], "+", 2)[0]
    		local = strings.ReplaceAll(local, ".", "")
    		domain := email[i:]
    		s[local+domain] = true
    	}
    	return len(s)
    }
    
  • function numUniqueEmails(emails: string[]): number {
        return new Set(
            emails
                .map(email => email.split('@'))
                .map(([start, end]) => start.replace(/\+.*|\./g, '') + '@' + end),
        ).size;
    }
    
    
  • const numUniqueEmails2 = function (emails) {
        const emailFilter = function (str) {
            let index = str.search(/@/);
            let s = str.substring(0, index);
            let s2 = str.substring(index + 1, str.length);
            let res = '';
            for (let i = 0; i < s.length; i++) {
                if (s[i] === '+') break;
                if (s[i] === '.') continue;
                res = res + s[i];
            }
            return res + s2;
        };
    
        let arr = [];
        for (let i = 0; i < emails.length; i++) {
            let t = emailFilter(emails[i]);
            if (arr.indexOf(t) === -1) {
                arr.push(t);
            }
        }
        return arr.length;
    };
    
    const numUniqueEmails = function (emails) {
        let arr = emails.map(str => {
            let index = str.search(/@/);
            let s = str.substring(0, index);
            let s2 = str.substring(index + 1, str.length);
            let res = '';
            for (let i = 0; i < s.length; i++) {
                if (s[i] === '+') break;
                if (s[i] === '.') continue;
                res = res + s[i];
            }
            return res + s2;
        });
        let set = new Set(arr);
        return set.size;
    };
    
    
  • use std::collections::HashSet;
    impl Solution {
        pub fn num_unique_emails(emails: Vec<String>) -> i32 {
            let mut set = HashSet::new();
            for email in emails.iter() {
                let res: Vec<&str> = email.split('@').collect();
                let mut s = String::new();
                for &c in res[0].as_bytes().iter() {
                    if c == b'.' {
                        continue;
                    }
                    if c == b'+' {
                        break;
                    }
                    s.push(c as char);
                }
                s.push('@');
                s.push_str(res[1]);
                set.insert(s);
            }
            set.len() as i32
        }
    }
    
    

All Problems

All Solutions