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
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
3public class ValidEmailsCheck {
4 public static List<Map<String, String>> findValidEmails(List<Map<String, String>> users) {
5 List<Map<String, String>> validEmails = new ArrayList<>();
6
7 for (Map<String, String> user : users) {
8 String email = user.get("mail");
9 if (isValidEmail(email)) {
10 validEmails.add(user);
11 }
12 }
13 return validEmails;
14 }
15
16 private static boolean isValidEmail(String email) {
17 if (!email.contains("@leetcode.com")) {
18 return false;
19 }
20 String[] parts = email.split("@", 2);
21 if (parts.length != 2 || !parts[1].equals("leetcode.com")) {
22 return false;
23 }
24
25 String prefix = parts[0];
26 if (prefix.isEmpty() || !Character.isLetter(prefix.charAt(0))) {
27 return false;
28 }
29 for (char c : prefix.toCharArray()) {
30 if (!Character.isLetterOrDigit(c) && c != '.' && c != '_' && c != '-') {
31 return false;
32 }
33 }
34 return true;
35 }
36
37 public static void main(String[] args) {
38 List<Map<String, String>> users = new ArrayList<>();
39 users.add(new HashMap<>(Map.of("user_id", "1", "name", "Winston", "mail", "winston@leetcode.com")));
40 users.add(new HashMap<>(Map.of("user_id", "2", "name", "Jonathan", "mail", "jonathanisgreat")));
41 users.add(new HashMap<>(Map.of("user_id", "3", "name", "Annabelle", "mail", "bella-@leetcode.com")));
42 users.add(new HashMap<>(Map.of("user_id", "4", "name", "Sally", "mail", "sally.come@leetcode.com")));
43 users.add(new HashMap<>(Map.of("user_id", "5", "name", "Marwan", "mail", "quarz#2020@leetcode.com")));
44 users.add(new HashMap<>(Map.of("user_id", "6", "name", "David", "mail", "david69@gmail.com")));
45 users.add(new HashMap<>(Map.of("user_id", "7", "name", "Shapiro", "mail", ".shapo@leetcode.com")));
46
47 List<Map<String, String>> validUsers = findValidEmails(users);
48 System.out.println(validUsers);
49 }
50}
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.