
Sponsored
Sponsored
In this approach, we use regular expressions to check for valid emails. The regular expression will ensure that the email format adheres to the specified rules: the prefix starts with a letter and consists only of permitted characters, followed by '@leetcode.com'. This method efficiently checks the constraints by leveraging regular expressions, which are well-suited for pattern matching tasks.
The time complexity of this solution is O(n), where n is the number of users, as each email is scanned once. The space complexity is O(n) as we store all valid users in a list.
1function findValidEmails(users) {
2 let validEmails = [];
3 let pattern = /^[a-zA-Z][a-zA-Z0-9_.-]*@leetcode\.com$/;
4
5 for (let user of users) {
6 if (pattern.test(user.mail)) {
7 validEmails.push(user);
8 }
9 }
10 return validEmails;
11}
12
13// Example usage
14const users = [
15 {"user_id": 1, "name": "Winston", "mail": "winston@leetcode.com"},
16 {"user_id": 2, "name": "Jonathan", "mail": "jonathanisgreat"},
17 {"user_id": 3, "name": "Annabelle", "mail": "bella-@leetcode.com"},
18 {"user_id": 4, "name": "Sally", "mail": "sally.come@leetcode.com"},
19 {"user_id": 5, "name": "Marwan", "mail": "quarz#2020@leetcode.com"},
20 {"user_id": 6, "name": "David", "mail": "david69@gmail.com"},
21 {"user_id": 7, "name": "Shapiro", "mail": ".shapo@leetcode.com"}
22];
23
24console.log(findValidEmails(users));A JavaScript solution using the 'RegExp' object to verify if each email matches the specified pattern. It uses a similar filtering approach as those in other languages.
This approach involves manually parsing the email to check the prefix and domain components. We split the email at the '@' character and validate both parts separately. This is an alternative to regex that provides a more step-by-step checking method.
The time complexity is O(n * m), where n is the number of users and m is the length of the largest email string due to manual character checking. The space complexity is O(n) for the result list.
1import java.util.*;
2
3
This Java solution manually parses the email to separate the prefix and domain parts. It checks if the domain matches '@leetcode.com' and validates the prefix according to rules. This approach foregoes regex for explicit parsing and logical checking.