Unique Email Addresses


Every email consists of a local name and a domain name, separated by the @ sign.

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

Besides lowercase letters, these emails may contain '.'s or '+'s.

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. For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address. (Note that this rule does not apply for domain names.)

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, for example m.y+name@email.com will be forwarded to my@email.com. (Again, this rule does not apply for domain names.)

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

Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?


  • 1 <= emails[i].length <= 100.
  • 1 <= emails.length <= 100
  • Each emails[i] contains exactly one ‘@’ character.

See on Leetcode


class Solution {
    public int numUniqueEmails(String[] emails) {

        HashSet < String > actualEmails = new HashSet();

        /* Iterate over all of the emails, split the email addresses by "@"
           to find the local name and domain. Ignore the text after "+" in
           the local name and a remove "." from the local name. Append this
           mutated local name to the domain and insert into a HashSet to
           eliminate duplicates. The size of the HashSet would be the
           number of unique addresses.
        for (int i = 0; i < emails.length; i++) {

            StringBuffer finalEmail = new StringBuffer();

            // separate local name and domain name
            String[] localAndDomain = emails[i].split("@", 2);

            // is it a bad email? break!
            if (localAndDomain.length != 2) {

            String localNameUF = localAndDomain[0]; //the local name
            String domainName = localAndDomain[1]; // the domain name

            // split local name by "+"
            String[] localNamePlus = localNameUF.split("\\+", 2);

            // pick first element from local name  array after split by '+'
            String localNameF = localNamePlus[0];

            // remove the "." occurrences from local name
            String localName = localNameF.replace(".", "");

            //Recreate the email by appending the mutated local name with domain name

            // add the email to hash set

      // return the number of email addresses
        return actualEmails.size();

Test Cases

  • An email with a ‘.’ in the local name for eg. joe.joe@domainname.com
  • An email with a ‘+’ in the local name for eg. joe+joe@domainname.com
  • An email with a ‘.’ and ‘+’ in the local name and domain name for eg. joe.joe+john@domain.name+parent.com
  • An email with a consecutive ‘.’ and ‘+’ in the local name for eg. joe.+joe@domainname.com
  • An email with no ‘.’ and ‘+’ in the local name for eg. joe@domainname.com
  • Invalid input string, an email with no ‘@’ and with ‘.’ and ‘+’ for eg. joe.joe+joe.
  • An email with a ‘.’ before and after ‘+’ in the local name also numbers in the email for eg. joe.joe+11.11@domainname.com.
  • Empty or Null input (this condition is not handled in current code).

See also