Unique Email Addresses Leetcode Problem 929 [Python Solution]
In this LeetCode problem, Unique Email Addresses, we are tasked with counting the number of unique email addresses in a given list of email strings.
To determine uniqueness, we need to consider certain rules:
- Email addresses consist of a local name and a domain name, separated by the ‘@’ symbol.
- In the local name part of an email address, periods (‘.’) are ignored.
This rule does not apply to domain names.
- If a plus (‘+’) sign appears in the local name, everything after the first plus sign is ignored.
This rule also does not apply to domain names.
- It’s possible to use both rules at the same time.
Let’s illustrate these rules with examples:
- “[email protected]” and “[email protected]” are considered the same address.
- “[email protected]” and “[email protected]” are also considered the same.
- “[email protected]” simplifies to “[email protected]”.
Given an array of strings emails
, our goal is to return the number of different unique addresses that receive emails.
Example 1:
Input: emails = ["[email protected]","[email protected]","[email protected]"]
Output: 2
Explanation: "[email protected]" and "[email protected]" are the unique email addresses.
Understanding Constraints
Before we delve into the solution, let’s understand the constraints:
- 1 <= emails.length <= 100
- 1 <= emails[i].length <= 100
- emails[i] consists of lowercase English letters, ‘+’, ‘.’, and ‘@’.
- Each emails[i] contains exactly one ‘@’ character.
- Local names do not start with a ‘+’ character.
- Domain names end with the “.com” suffix.
Efficient Python Code Solution
def numUniqueEmails(emails: list[str]) -> int:
unique_emails: set[str] = set()
for email in emails:
local_name, domain_name = email.split('@')
local_name = local_name.split('+')[0]
local_name = local_name.replace('.', '')
email = local_name + '@' + domain_name
unique_emails.add(email)
return len(unique_emails)
Time and Space Complexity
The time complexity of this solution is O(N*M)
, where N is the number of emails, and M is the average size of each email string.
We iterate through each character in the email to simplify it.
The set
operations are generally O(1)
.
The space complexity is O(N)
since we store unique emails in a set.
Related Interview Questions By Company:
Related Interview Questions By Difficulty:
Related Interview Questions By Category:
Reasoning Behind Our Approach
We have solved this problem by iterating through each email in the list, applying the rules to simplify the local name, and then storing the simplified email in a set to ensure uniqueness.
This approach allows us to efficiently count the number of unique email addresses in the list.
In conclusion, the Unique Email Addresses problem is a straightforward task with a Python solution that leverages string manipulation and a set data structure to handle unique addresses.
It’s important to understand the rules for email simplification and how to apply them effectively to achieve the desired result.
For more practice and to submit your solution, you can check the problem on LeetCode.
Remember to comment, ask questions, make suggestions, and share this content to help others in their coding journey!