929. Unique Email Addresses
Explanation:
To solve this problem, we need to iterate through each email address and process it according to the given rules. We can achieve this by splitting each email address into the local name and domain name, then applying the necessary transformations to the local name. After that, we can store the unique transformed email addresses in a set data structure to keep track of the distinct addresses.
-
For each email address:
- Split the email address into the local name and domain name.
- Process the local name:
- Remove all '.' characters.
- Ignore all characters after the first '+' character.
- Combine the processed local name with the domain name to form the transformed email address.
- Add the transformed email address to a set.
-
Finally, return the size of the set, which represents the number of different addresses that actually receive mails.
Time Complexity:
- The time complexity of this approach is O(n*m), where n is the number of email addresses and m is the average length of an email address.
Space Complexity:
- The space complexity is also O(n*m) due to the set used to store unique email addresses.
:
class Solution {
public int numUniqueEmails(String[] emails) {
Set<String> uniqueEmails = new HashSet<>();
for (String email : emails) {
String[] parts = email.split("@");
String localName = parts[0].split("\\+")[0].replace(".", "");
uniqueEmails.add(localName + "@" + parts[1]);
}
return uniqueEmails.size();
}
}
Code Editor (Testing phase)
Improve Your Solution
Use the editor below to refine the provided solution. Select a programming language and try the following:
- Add import statement if required.
- Optimize the code for better time or space complexity.
- Add test cases to validate edge cases and common scenarios.
- Handle error conditions or invalid inputs gracefully.
- Experiment with alternative approaches to deepen your understanding.
Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.