Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d36c5a9

Browse files
feat: add solutions to lc problem: No.3436 (doocs#4004)
No.3436.Find Valid Emails
1 parent c461e7b commit d36c5a9

File tree

8 files changed

+269
-0
lines changed

8 files changed

+269
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3436.Find%20Valid%20Emails/README.md
5+
tags:
6+
- 数据库
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3436. Find Valid Emails](https://leetcode.cn/problems/find-valid-emails)
12+
13+
[English Version](/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md)
14+
15+
## 题目描述
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&#39;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>&nbsp;</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&nbsp;is alphanumeric, and example.com&nbsp;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+
## 解法
89+
90+
<!-- solution:start -->
91+
92+
### 方法一:正则表达式
93+
94+
我们可以使用正则表达式,通过 `REGEXP` 来匹配符合条件的邮箱地址。
95+
96+
<!-- tabs:start -->
97+
98+
#### MySQL
99+
100+
```sql
101+
# Write your MySQL query statement below
102+
SELECT user_id, email
103+
FROM Users
104+
WHERE email REGEXP '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\\.com$'
105+
ORDER BY 1;
106+
```
107+
108+
#### Pandas
109+
110+
```python
111+
import pandas as pd
112+
113+
114+
def find_valid_emails(users: pd.DataFrame) -> pd.DataFrame:
115+
email_pattern = r"^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\.com$"
116+
valid_emails = users[users["email"].str.match(email_pattern)]
117+
valid_emails = valid_emails.sort_values(by="user_id")
118+
return valid_emails
119+
```
120+
121+
<!-- tabs:end -->
122+
123+
<!-- solution:end -->
124+
125+
<!-- problem:end -->
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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&#39;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>&nbsp;</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&nbsp;is alphanumeric, and example.com&nbsp;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 -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pandas as pd
2+
3+
4+
def find_valid_emails(users: pd.DataFrame) -> pd.DataFrame:
5+
email_pattern = r"^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\.com$"
6+
valid_emails = users[users["email"].str.match(email_pattern)]
7+
valid_emails = valid_emails.sort_values(by="user_id")
8+
return valid_emails
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Write your MySQL query statement below
2+
SELECT user_id, email
3+
FROM Users
4+
WHERE email REGEXP '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\\.com$'
5+
ORDER BY 1;

‎solution/DATABASE_README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@
308308
| 3401 | [Find Circular Gift Exchange Chains](/solution/3400-3499/3401.Find%20Circular%20Gift%20Exchange%20Chains/README.md) | `数据库` | 困难 | 🔒 |
309309
| 3415 | [查找具有三个连续数字的产品](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README.md) | `数据库` | 简单 | 🔒 |
310310
| 3421 | [查找进步的学生](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README.md) | `数据库` | 中等 | |
311+
| 3436 | [Find Valid Emails](/solution/3400-3499/3436.Find%20Valid%20Emails/README.md) | | 简单 | |
311312

312313
## 版权
313314

‎solution/DATABASE_README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
306306
| 3401 | [Find Circular Gift Exchange Chains](/solution/3400-3499/3401.Find%20Circular%20Gift%20Exchange%20Chains/README_EN.md) | `Database` | Hard | 🔒 |
307307
| 3415 | [Find Products with Three Consecutive Digits](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README_EN.md) | `Database` | Easy | 🔒 |
308308
| 3421 | [Find Students Who Improved](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README_EN.md) | `Database` | Medium | |
309+
| 3436 | [Find Valid Emails](/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md) | | Easy | |
309310

310311
## Copyright
311312

‎solution/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3446,6 +3446,7 @@
34463446
| 3433 | [统计用户被提及情况](/solution/3400-3499/3433.Count%20Mentions%20Per%20User/README.md) | `数组`,`数学`,`排序`,`模拟` | 中等 | 第 434 场周赛 |
34473447
| 3434 | [子数组操作后的最大频率](/solution/3400-3499/3434.Maximum%20Frequency%20After%20Subarray%20Operation/README.md) | `贪心`,`数组`,`哈希表`,`动态规划`,`前缀和` | 中等 | 第 434 场周赛 |
34483448
| 3435 | [最短公共超序列的字母出现频率](/solution/3400-3499/3435.Frequencies%20of%20Shortest%20Supersequences/README.md) | `位运算`,`图`,`拓扑排序`,`数组`,`字符串`,`枚举` | 困难 | 第 434 场周赛 |
3449+
| 3436 | [Find Valid Emails](/solution/3400-3499/3436.Find%20Valid%20Emails/README.md) | | 简单 | |
34493450

34503451
## 版权
34513452

‎solution/README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,6 +3444,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
34443444
| 3433 | [Count Mentions Per User](/solution/3400-3499/3433.Count%20Mentions%20Per%20User/README_EN.md) | `Array`,`Math`,`Sorting`,`Simulation` | Medium | Weekly Contest 434 |
34453445
| 3434 | [Maximum Frequency After Subarray Operation](/solution/3400-3499/3434.Maximum%20Frequency%20After%20Subarray%20Operation/README_EN.md) | `Greedy`,`Array`,`Hash Table`,`Dynamic Programming`,`Prefix Sum` | Medium | Weekly Contest 434 |
34463446
| 3435 | [Frequencies of Shortest Supersequences](/solution/3400-3499/3435.Frequencies%20of%20Shortest%20Supersequences/README_EN.md) | `Bit Manipulation`,`Graph`,`Topological Sort`,`Array`,`String`,`Enumeration` | Hard | Weekly Contest 434 |
3447+
| 3436 | [Find Valid Emails](/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md) | | Easy | |
34473448

34483449
## Copyright
34493450

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /