Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 '+'.
"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.
"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.
"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.
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 <= 1001 <= emails[i].length <= 100emails[i] consist of lowercase English letters, '+', '.' and '@'.emails[i] contains exactly one '@' character.'+' character.".com" suffix.".com" suffix.Problem summary: 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #929: Unique Email Addresses
class Solution {
public int numUniqueEmails(String[] emails) {
Set<String> s = new HashSet<>();
for (String email : emails) {
String[] parts = email.split("@");
String local = parts[0];
String domain = parts[1];
StringBuilder t = new StringBuilder();
for (char c : local.toCharArray()) {
if (c == '.') {
continue;
}
if (c == '+') {
break;
}
t.append(c);
}
s.add(t.toString() + "@" + domain);
}
return s.size();
}
}
// Accepted solution for LeetCode #929: Unique Email Addresses
func numUniqueEmails(emails []string) int {
s := make(map[string]struct{})
for _, email := range emails {
parts := strings.Split(email, "@")
local := parts[0]
domain := parts[1]
var t strings.Builder
for _, c := range local {
if c == '.' {
continue
}
if c == '+' {
break
}
t.WriteByte(byte(c))
}
s[t.String()+"@"+domain] = struct{}{}
}
return len(s)
}
# Accepted solution for LeetCode #929: Unique Email Addresses
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
s = set()
for email in emails:
local, domain = email.split("@")
t = []
for c in local:
if c == ".":
continue
if c == "+":
break
t.append(c)
s.add("".join(t) + "@" + domain)
return len(s)
// Accepted solution for LeetCode #929: Unique Email Addresses
use std::collections::HashSet;
impl Solution {
pub fn num_unique_emails(emails: Vec<String>) -> i32 {
let mut s = HashSet::new();
for email in emails {
let parts: Vec<&str> = email.split('@').collect();
let local = parts[0];
let domain = parts[1];
let mut t = String::new();
for c in local.chars() {
if c == '.' {
continue;
}
if c == '+' {
break;
}
t.push(c);
}
s.insert(format!("{}@{}", t, domain));
}
s.len() as i32
}
}
// Accepted solution for LeetCode #929: Unique Email Addresses
function numUniqueEmails(emails: string[]): number {
const s = new Set<string>();
for (const email of emails) {
const [local, domain] = email.split('@');
let t = '';
for (const c of local) {
if (c === '.') {
continue;
}
if (c === '+') {
break;
}
t += c;
}
s.add(t + '@' + domain);
}
return s.size;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.