LeetCode Solutions

929. Unique Email Addresses

Time: $O(n)$

Space: $O(n)$

			

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();
  }
};
			

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();
  }
}
			

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)