Advertisement

Unique Email Addresses - LeetCode 929 Solution

Unique Email Addresses - Complete Solution Guide

Unique Email Addresses is LeetCode problem 929, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 "[email protected]" , "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 .

Detailed Explanation

The problem requires you to process a list of email addresses and determine the number of unique email addresses that will actually receive mail after applying specific rules. These rules involve handling '.' characters (which are ignored) and '+' characters (where everything after the '+' in the local name is ignored) in the local name part of the email address. The domain name remains unchanged. The goal is to count how many distinct email addresses result after these transformations.

Solution Approach

The solution iterates through each email in the input list, processes the local name according to the given rules (removing '.' and truncating at '+'), and then reconstructs the normalized email address. A set is used to store these normalized email addresses, ensuring that only unique addresses are counted. Finally, the size of the set represents the number of unique email addresses.

Step-by-Step Algorithm

  1. Step 1: Iterate through each email address in the input list.
  2. Step 2: Split each email address into local name and domain name using the '@' character as a delimiter.
  3. Step 3: Process the local name by first finding the index of the '+' character and truncating the local name at that point. If '+' doesn't exist then process the entire local name.
  4. Step 4: Remove all '.' characters from the local name.
  5. Step 5: Concatenate the processed local name, '@', and the domain name to form the normalized email address.
  6. Step 6: Add the normalized email address to a set.
  7. Step 7: After processing all email addresses, return the size of the set, which represents the number of unique email addresses.

Key Insights

  • Insight 1: Separating the local and domain names is crucial for applying the email processing rules.
  • Insight 2: The rules only apply to the local name part of the email address; the domain name is not affected.
  • Insight 3: Using a set is essential to efficiently count the unique email addresses after processing.

Complexity Analysis

Time Complexity: O(n*m)

Space Complexity: O(n*m)

Topics

This problem involves: Array, Hash Table, String.

Companies

Asked at: Intuit, Twitch, Wix.