|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md |
| 5 | +tags: |
| 6 | + - Database |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- problem:start --> |
| 10 | + |
| 11 | +# [3436. Find Valid Emails](https://leetcode.com/problems/find-valid-emails) |
| 12 | + |
| 13 | +[中文文档](/solution/3400-3499/3436.Find%20Valid%20Emails/README.md) |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Table: <code>Users</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++-----------------+---------+ |
| 23 | +| Column Name | Type | |
| 24 | ++-----------------+---------+ |
| 25 | +| user_id | int | |
| 26 | +| email | varchar | |
| 27 | ++-----------------+---------+ |
| 28 | +(user_id) is the unique key for this table. |
| 29 | +Each row contains a user's unique ID and email address. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p>Write a solution to find all the <strong>valid email addresses</strong>. A valid email address meets the following criteria:</p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li>It contains exactly one <code>@</code> symbol.</li> |
| 36 | + <li>The part before the <code>@</code> symbol contains only <strong>alphanumeric</strong> characters and <strong>underscores</strong>.</li> |
| 37 | + <li>The part after the <code>@</code> symbol contains a domain name <strong>that starts with a letter</strong> and ends with <code>.com</code>.</li> |
| 38 | +</ul> |
| 39 | + |
| 40 | +<p>Return<em> the result table ordered by</em> <code>user_id</code> <em>in</em> <strong>ascending </strong><em>order</em>.</p> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong class="example">Example:</strong></p> |
| 44 | + |
| 45 | +<div class="example-block"> |
| 46 | +<p><strong>Input:</strong></p> |
| 47 | + |
| 48 | +<p>Users table:</p> |
| 49 | + |
| 50 | +<pre class="example-io"> |
| 51 | ++---------+---------------------+ |
| 52 | +| user_id | email | |
| 53 | ++---------+---------------------+ |
| 54 | +| 1 | alice@example.com | |
| 55 | +| 2 | bob_at_example.com | |
| 56 | +| 3 | charlie@example.net | |
| 57 | +| 4 | david@domain.com | |
| 58 | +| 5 | eve@invalid | |
| 59 | ++---------+---------------------+ |
| 60 | +</pre> |
| 61 | + |
| 62 | +<p><strong>Output:</strong></p> |
| 63 | + |
| 64 | +<pre class="example-io"> |
| 65 | ++---------+-------------------+ |
| 66 | +| user_id | email | |
| 67 | ++---------+-------------------+ |
| 68 | +| 1 | alice@example.com | |
| 69 | +| 4 | david@domain.com | |
| 70 | ++---------+-------------------+ |
| 71 | +</pre> |
| 72 | + |
| 73 | +<p><strong>Explanation:</strong></p> |
| 74 | + |
| 75 | +<ul> |
| 76 | + <li><strong>alice@example.com</strong> is valid because it contains one <code>@</code>, alice is alphanumeric, and example.com starts with a letter and ends with .com.</li> |
| 77 | + <li><strong>bob_at_example.com</strong> is invalid because it contains an underscore instead of an <code>@</code>.</li> |
| 78 | + <li><strong>charlie@example.net</strong> is invalid because the domain does not end with <code>.com</code>.</li> |
| 79 | + <li><strong>david@domain.com</strong> is valid because it meets all criteria.</li> |
| 80 | + <li><strong>eve@invalid</strong> is invalid because the domain does not end with <code>.com</code>.</li> |
| 81 | +</ul> |
| 82 | + |
| 83 | +<p>Result table is ordered by user_id in ascending order.</p> |
| 84 | +</div> |
| 85 | + |
| 86 | +<!-- description:end --> |
| 87 | + |
| 88 | +## Solutions |
| 89 | + |
| 90 | +<!-- solution:start --> |
| 91 | + |
| 92 | +### Solution 1: Regular Expression |
| 93 | + |
| 94 | +We can use a regular expression with `REGEXP` to match valid email addresses. |
| 95 | + |
| 96 | +The time complexity is $O(n),ドル and the space complexity is $O(1)$. Here, $n$ is the length of the input string. |
| 97 | + |
| 98 | +<!-- tabs:start --> |
| 99 | + |
| 100 | +#### MySQL |
| 101 | + |
| 102 | +```sql |
| 103 | +# Write your MySQL query statement below |
| 104 | +SELECT user_id, email |
| 105 | +FROM Users |
| 106 | +WHERE email REGEXP '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\\.com$' |
| 107 | +ORDER BY 1; |
| 108 | +``` |
| 109 | + |
| 110 | +#### Pandas |
| 111 | + |
| 112 | +```python |
| 113 | +import pandas as pd |
| 114 | + |
| 115 | + |
| 116 | +def find_valid_emails(users: pd.DataFrame) -> pd.DataFrame: |
| 117 | + email_pattern = r"^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\.com$" |
| 118 | + valid_emails = users[users["email"].str.match(email_pattern)] |
| 119 | + valid_emails = valid_emails.sort_values(by="user_id") |
| 120 | + return valid_emails |
| 121 | +``` |
| 122 | + |
| 123 | +<!-- tabs:end --> |
| 124 | + |
| 125 | +<!-- solution:end --> |
| 126 | + |
| 127 | +<!-- problem:end --> |
0 commit comments