Skip to content

929. Unique Email Addresses 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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();
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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();
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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)