Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/UniqueEmailAddresses929.java

This file was deleted.

54 changes: 54 additions & 0 deletions src/main/java/com/leetcode/easy/UniqueEmailAddresses929.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Tags: Array, String, Hash Set
package com.leetcode.easy;

import java.util.HashSet;
import java.util.Set;

public class UniqueEmailAddresses929 {

/**
* Returns the number of unique email addresses after applying normalization rules.
* <p>
* Normalization rules:
* <ul>
* <li>Dots (.) in the local name are ignored</li>
* <li>Everything after the first plus (+) in the local name is ignored</li>
* </ul>
* <p>
* <b>Time Complexity: O(N × L)</b>
* <ul>
* <li>N = number of emails in the input array</li>
* <li>L = average length of an email address</li>
* <li>For each email, we perform:
* <ul>
* <li>split("@") - O(L) to scan and split the string</li>
* <li>replace(".", "") - O(L) to create new string without dots</li>
* <li>split("\\+") - O(L) to find and split at '+' character</li>
* <li>HashSet.add() - O(L) for hashing the string and O(1) average insertion</li>
* </ul>
* </li>
* <li>Total: O(N × L) since we process each character of each email a constant number of times</li>
* </ul>
* <p>
* <b>Space Complexity: O(N × L)</b>
* <ul>
* <li>HashSet stores up to N unique normalized emails</li>
* <li>Each normalized email has length O(L) in the worst case</li>
* <li>Additional temporary strings (split arrays, replaced strings) are O(L) each and get garbage collected</li>
* <li>Worst case: all N emails are unique → O(N × L) total storage</li>
* <li>Best case: all N emails normalize to the same address → O(L) storage</li>
* </ul>
*
* @param emails array of email addresses to process
* @return the count of unique email addresses
*/
public int numUniqueEmails(String[] emails) {
Set<String> emailHashSet = new HashSet<>();
for (String email : emails) {
String[] parts = email.split("@");
String localName = parts[0].replace(".", "").split("\\+")[0];
emailHashSet.add(localName + "@" + parts[1]);
}
return emailHashSet.size();
}
}
35 changes: 35 additions & 0 deletions src/test/java/com/leetcode/easy/UniqueEmailAddresses929Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.leetcode.easy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class UniqueEmailAddresses929Test {
private UniqueEmailAddresses929 solution;

@BeforeEach
void setUp() {
solution = new UniqueEmailAddresses929();
}

@Test
void testExample1() {
String[] emails = {
"test.email+alex@leetcode.com",
"test.e.mail+bob.cathy@leetcode.com",
"testemail+david@lee.tcode.com"
};
assertEquals(2, solution.numUniqueEmails(emails));
}

@Test
void testExample2() {
String[] emails = {
"a@leetcode.com",
"b@leetcode.com",
"c@leetcode.com"
};
assertEquals(3, solution.numUniqueEmails(emails));
}
}
Loading