929. Unique Email Addresses LeetCode Solution

In this guide, you will get 929. Unique Email Addresses LeetCode Solution with the best time and space complexity. The solution to Unique Email Addresses problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Unique Email Addresses solution in C++
  4. Unique Email Addresses solution in Java
  5. Unique Email Addresses solution in Python
  6. Additional Resources
929. Unique Email Addresses LeetCode Solution image

Problem Statement of Unique Email Addresses

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.

See also  771. Jewels and Stones LeetCode Solution

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.
Domain names must contain at least one character before ".com" suffix.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

929. Unique Email Addresses LeetCode Solution in C++

class Solution {
 public:
  int numUniqueEmails(vector<string>& emails) {
    unordered_set<string> normalized;

    for (const string& email : emails) {
      string local;
      for (const char c : email) {
        if (c == '+' || c == '@')
          break;
        if (c == '.')
          continue;
        local += c;
      }
      string atDomain = email.substr(email.find('@'));
      normalized.insert(local + atDomain);
    }

    return normalized.size();
  }
};
/* code provided by PROGIEZ */

929. Unique Email Addresses LeetCode Solution in Java

class Solution {
  public int numUniqueEmails(String[] emails) {
    Set<String> normalized = new HashSet<>();

    for (final String email : emails) {
      String[] parts = email.split("@");
      String[] local = parts[0].split("\\+");
      normalized.add(local[0].replace(".", "") + "@" + parts[1]);
    }

    return normalized.size();
  }
}
// code provided by PROGIEZ

929. Unique Email Addresses LeetCode Solution in Python

class Solution:
  def numUniqueEmails(self, emails: list[str]) -> int:
    seen = set()

    for email in emails:
      local, domain = email.split('@')
      local = local.split('+')[0].replace('.', '')
      seen.add(local + '@' + domain)

    return len(seen)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.