Skip to main content
Back to problems
#3860
Medium Algorithms

Unique email groups

Array Hash Table String
87.9% acceptance
Apr 3, 2026
6
2
You are given an array of strings emails, where each string is a valid email address. Two email addresses belong to the same group if both their normalized local names and normalized domain names are identical. The normalization rules are as follows: The local name is the part before the '@' symbol. Ignore all dots '.'. Ignore everything after the first '+', if present. Convert to lowercase. The domain name is the part after the '@' symbol. Convert to lowercase. Return an integer denoting the number of unique email groups after normalization.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn unique_email_groups(emails: Vec<String>) -> i32 {
    let mut groups = std::collections::HashSet::new();

    for email in emails {
      let (local, domain) = email.split_once('@').unwrap();
      let mut normalized_local = String::with_capacity(local.len());

      for ch in local.chars() {
        match ch {
          '+' => break,
          '.' => {}
          _ => normalized_local.push(ch.to_ascii_lowercase()),
        }
      }

      groups.insert(format!("{}@{}", normalized_local, domain.to_ascii_lowercase()));
    }

    groups.len() as i32
  }
}