Similar Problems

Similar Problems not available

Unique Email Addresses - Leetcode Solution

Companies:

LeetCode:  Unique Email Addresses Leetcode Solution

Difficulty: Easy

Topics: string hash-table array  

The problem Unique Email Addresses on LeetCode is a string manipulation problem that requires you to process a list of email addresses and return the number of unique addresses. The problem statement and criteria for this problem are as follows:

Problem Statement:

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.

  • 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, for example, "+ignoreme@leetcode.com" will be filtered.

  • For example, "alice+bob@leetcode.com" and "alice@leetcode.com" forward to the same email address.

  • Return the number of different addresses that actually receive mails.

Constraints:

  • 1 <= emails.length <= 100
  • 1 <= emails[i].length <= 100
  • email[i] consist of lowercase English letters, '+', '.' and '@'.
  • Each emails[i] contains exactly one '@' character.

Solution:

To solve this problem, we can start by processing each email address in the list and separating it into its local name and domain name. We can then apply the rules for processing the local name and generate the actual email addresses that will receive mail. Finally, we can count the number of unique email addresses and return the count.

Steps:

  • Create a HashSet to store the unique email addresses.
  • Loop through each email address in the list.
  • Split the email address into the local name and domain name using the @ symbol.
  • Process the local name according to the rules:
    • Remove all occurrences of '.' in the local name.
    • Remove all characters after the first occurrence of '+' in the local name.
  • Concatenate the processed local name and domain name with the @ symbol to form the actual email address that will receive mail.
  • Add the actual email address to the HashSet.
  • After processing all email addresses, return the size of the HashSet.

Here is the Java code implementation for this solution:

class Solution { public int numUniqueEmails(String[] emails) { Set<String> uniqueEmails = new HashSet<>(); for (String email : emails) { // Split the email into local name and domain name String[] parts = email.split("@"); String localName = parts[0]; String domainName = parts[1]; // Process the local name localName = localName.replace(".", ""); int plusIndex = localName.indexOf("+"); if (plusIndex != -1) { localName = localName.substring(0, plusIndex); } // Concatenate the processed local name and domain name String actualEmail = localName + "@" + domainName; uniqueEmails.add(actualEmail); } return uniqueEmails.size(); } }

Time Complexity: O(n * m) where n is the number of email addresses in the list and m is the maximum length of an email address.

Space Complexity: O(n) where n is the number of unique email addresses.

Unique Email Addresses Solution Code

1