Skip to content

feat: add solutions to lc problem: No.3436 #4004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions solution/3400-3499/3436.Find Valid Emails/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3436.Find%20Valid%20Emails/README.md
tags:
- 数据库
---

<!-- problem:start -->

# [3436. Find Valid Emails](https://leetcode.cn/problems/find-valid-emails)

[English Version](/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md)

## 题目描述

<!-- description:start -->

<p>Table: <code>Users</code></p>

<pre>
+-----------------+---------+
| Column Name | Type |
+-----------------+---------+
| user_id | int |
| email | varchar |
+-----------------+---------+
(user_id) is the unique key for this table.
Each row contains a user&#39;s unique ID and email address.
</pre>

<p>Write a solution to find all the <strong>valid email addresses</strong>. A valid email address meets the following criteria:</p>

<ul>
<li>It contains exactly one <code>@</code> symbol.</li>
<li>The part before the <code>@</code> symbol contains only <strong>alphanumeric</strong> characters and <strong>underscores</strong>.</li>
<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>
</ul>

<p>Return<em> the result table ordered by</em> <code>user_id</code> <em>in</em> <strong>ascending </strong><em>order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p>Users table:</p>

<pre class="example-io">
+---------+---------------------+
| user_id | email |
+---------+---------------------+
| 1 | alice@example.com |
| 2 | bob_at_example.com |
| 3 | charlie@example.net |
| 4 | david@domain.com |
| 5 | eve@invalid |
+---------+---------------------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+---------+-------------------+
| user_id | email |
+---------+-------------------+
| 1 | alice@example.com |
| 4 | david@domain.com |
+---------+-------------------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<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>
<li><strong>bob_at_example.com</strong> is invalid because it contains an underscore instead of an <code>@</code>.</li>
<li><strong>charlie@example.net</strong> is invalid because the domain does not end with <code>.com</code>.</li>
<li><strong>david@domain.com</strong> is valid because it meets all criteria.</li>
<li><strong>eve@invalid</strong> is invalid because the domain does not end with <code>.com</code>.</li>
</ul>

<p>Result table is ordered by user_id in ascending order.</p>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:正则表达式

我们可以使用正则表达式,通过 `REGEXP` 来匹配符合条件的邮箱地址。

<!-- tabs:start -->

#### MySQL

```sql
# Write your MySQL query statement below
SELECT user_id, email
FROM Users
WHERE email REGEXP '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\\.com$'
ORDER BY 1;
```

#### Pandas

```python
import pandas as pd


def find_valid_emails(users: pd.DataFrame) -> pd.DataFrame:
email_pattern = r"^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\.com$"
valid_emails = users[users["email"].str.match(email_pattern)]
valid_emails = valid_emails.sort_values(by="user_id")
return valid_emails
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
127 changes: 127 additions & 0 deletions solution/3400-3499/3436.Find Valid Emails/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md
tags:
- Database
---

<!-- problem:start -->

# [3436. Find Valid Emails](https://leetcode.com/problems/find-valid-emails)

[中文文档](/solution/3400-3499/3436.Find%20Valid%20Emails/README.md)

## Description

<!-- description:start -->

<p>Table: <code>Users</code></p>

<pre>
+-----------------+---------+
| Column Name | Type |
+-----------------+---------+
| user_id | int |
| email | varchar |
+-----------------+---------+
(user_id) is the unique key for this table.
Each row contains a user&#39;s unique ID and email address.
</pre>

<p>Write a solution to find all the <strong>valid email addresses</strong>. A valid email address meets the following criteria:</p>

<ul>
<li>It contains exactly one <code>@</code> symbol.</li>
<li>The part before the <code>@</code> symbol contains only <strong>alphanumeric</strong> characters and <strong>underscores</strong>.</li>
<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>
</ul>

<p>Return<em> the result table ordered by</em> <code>user_id</code> <em>in</em> <strong>ascending </strong><em>order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p>Users table:</p>

<pre class="example-io">
+---------+---------------------+
| user_id | email |
+---------+---------------------+
| 1 | alice@example.com |
| 2 | bob_at_example.com |
| 3 | charlie@example.net |
| 4 | david@domain.com |
| 5 | eve@invalid |
+---------+---------------------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+---------+-------------------+
| user_id | email |
+---------+-------------------+
| 1 | alice@example.com |
| 4 | david@domain.com |
+---------+-------------------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<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>
<li><strong>bob_at_example.com</strong> is invalid because it contains an underscore instead of an <code>@</code>.</li>
<li><strong>charlie@example.net</strong> is invalid because the domain does not end with <code>.com</code>.</li>
<li><strong>david@domain.com</strong> is valid because it meets all criteria.</li>
<li><strong>eve@invalid</strong> is invalid because the domain does not end with <code>.com</code>.</li>
</ul>

<p>Result table is ordered by user_id in ascending order.</p>
</div>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: Regular Expression

We can use a regular expression with `REGEXP` to match valid email addresses.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the input string.

<!-- tabs:start -->

#### MySQL

```sql
# Write your MySQL query statement below
SELECT user_id, email
FROM Users
WHERE email REGEXP '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\\.com$'
ORDER BY 1;
```

#### Pandas

```python
import pandas as pd


def find_valid_emails(users: pd.DataFrame) -> pd.DataFrame:
email_pattern = r"^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\.com$"
valid_emails = users[users["email"].str.match(email_pattern)]
valid_emails = valid_emails.sort_values(by="user_id")
return valid_emails
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
8 changes: 8 additions & 0 deletions solution/3400-3499/3436.Find Valid Emails/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pandas as pd


def find_valid_emails(users: pd.DataFrame) -> pd.DataFrame:
email_pattern = r"^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\.com$"
valid_emails = users[users["email"].str.match(email_pattern)]
valid_emails = valid_emails.sort_values(by="user_id")
return valid_emails
5 changes: 5 additions & 0 deletions solution/3400-3499/3436.Find Valid Emails/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Write your MySQL query statement below
SELECT user_id, email
FROM Users
WHERE email REGEXP '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9]*\\.com$'
ORDER BY 1;
1 change: 1 addition & 0 deletions solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
| 3401 | [Find Circular Gift Exchange Chains](/solution/3400-3499/3401.Find%20Circular%20Gift%20Exchange%20Chains/README.md) | `数据库` | 困难 | 🔒 |
| 3415 | [查找具有三个连续数字的产品](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README.md) | `数据库` | 简单 | 🔒 |
| 3421 | [查找进步的学生](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README.md) | `数据库` | 中等 | |
| 3436 | [Find Valid Emails](/solution/3400-3499/3436.Find%20Valid%20Emails/README.md) | | 简单 | |

## 版权

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

## Copyright

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

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3444,6 +3444,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3433 | [Count Mentions Per User](/solution/3400-3499/3433.Count%20Mentions%20Per%20User/README_EN.md) | `Array`,`Math`,`Sorting`,`Simulation` | Medium | Weekly Contest 434 |
| 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 |
| 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 |
| 3436 | [Find Valid Emails](/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md) | | Easy | |

## Copyright

Expand Down