From 8e47006e4430fe2e7d41f61eb0d2db49d7d6788f Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 30 Jan 2025 20:32:13 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3436 No.3436.Find Valid Emails --- .../3436.Find Valid Emails/README.md | 125 +++++++++++++++++ .../3436.Find Valid Emails/README_EN.md | 127 ++++++++++++++++++ .../3436.Find Valid Emails/Solution.py | 8 ++ .../3436.Find Valid Emails/Solution.sql | 5 + solution/DATABASE_README.md | 1 + solution/DATABASE_README_EN.md | 1 + solution/README.md | 1 + solution/README_EN.md | 1 + 8 files changed, 269 insertions(+) create mode 100644 solution/3400-3499/3436.Find Valid Emails/README.md create mode 100644 solution/3400-3499/3436.Find Valid Emails/README_EN.md create mode 100644 solution/3400-3499/3436.Find Valid Emails/Solution.py create mode 100644 solution/3400-3499/3436.Find Valid Emails/Solution.sql diff --git a/solution/3400-3499/3436.Find Valid Emails/README.md b/solution/3400-3499/3436.Find Valid Emails/README.md new file mode 100644 index 0000000000000..8c830fa15b63f --- /dev/null +++ b/solution/3400-3499/3436.Find Valid Emails/README.md @@ -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: + - 数据库 +--- + + + +# [3436. Find Valid Emails](https://leetcode.cn/problems/find-valid-emails) + +[English Version](/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md) + +## 题目描述 + + + +

Table: Users

+ +
++-----------------+---------+
+| Column Name     | Type    |
++-----------------+---------+
+| user_id         | int     |
+| email           | varchar |
++-----------------+---------+
+(user_id) is the unique key for this table.
+Each row contains a user's unique ID and email address.
+
+ +

Write a solution to find all the valid email addresses. A valid email address meets the following criteria:

+ + + +

Return the result table ordered by user_id in ascending order.

+ +

 

+

Example:

+ +
+

Input:

+ +

Users table:

+ +
++---------+---------------------+
+| user_id | email               |
++---------+---------------------+
+| 1       | alice@example.com   |
+| 2       | bob_at_example.com  |
+| 3       | charlie@example.net |
+| 4       | david@domain.com    |
+| 5       | eve@invalid         |
++---------+---------------------+
+
+ +

Output:

+ +
++---------+-------------------+
+| user_id | email             |
++---------+-------------------+
+| 1       | alice@example.com |
+| 4       | david@domain.com  |
++---------+-------------------+
+
+ +

Explanation:

+ + + +

Result table is ordered by user_id in ascending order.

+
+ + + +## 解法 + + + +### 方法一:正则表达式 + +我们可以使用正则表达式,通过 `REGEXP` 来匹配符合条件的邮箱地址。 + + + +#### 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 +``` + + + + + + diff --git a/solution/3400-3499/3436.Find Valid Emails/README_EN.md b/solution/3400-3499/3436.Find Valid Emails/README_EN.md new file mode 100644 index 0000000000000..a1c746a395bad --- /dev/null +++ b/solution/3400-3499/3436.Find Valid Emails/README_EN.md @@ -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 +--- + + + +# [3436. Find Valid Emails](https://leetcode.com/problems/find-valid-emails) + +[中文文档](/solution/3400-3499/3436.Find%20Valid%20Emails/README.md) + +## Description + + + +

Table: Users

+ +
++-----------------+---------+
+| Column Name     | Type    |
++-----------------+---------+
+| user_id         | int     |
+| email           | varchar |
++-----------------+---------+
+(user_id) is the unique key for this table.
+Each row contains a user's unique ID and email address.
+
+ +

Write a solution to find all the valid email addresses. A valid email address meets the following criteria:

+ + + +

Return the result table ordered by user_id in ascending order.

+ +

 

+

Example:

+ +
+

Input:

+ +

Users table:

+ +
++---------+---------------------+
+| user_id | email               |
++---------+---------------------+
+| 1       | alice@example.com   |
+| 2       | bob_at_example.com  |
+| 3       | charlie@example.net |
+| 4       | david@domain.com    |
+| 5       | eve@invalid         |
++---------+---------------------+
+
+ +

Output:

+ +
++---------+-------------------+
+| user_id | email             |
++---------+-------------------+
+| 1       | alice@example.com |
+| 4       | david@domain.com  |
++---------+-------------------+
+
+ +

Explanation:

+ + + +

Result table is ordered by user_id in ascending order.

+
+ + + +## Solutions + + + +### 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. + + + +#### 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 +``` + + + + + + diff --git a/solution/3400-3499/3436.Find Valid Emails/Solution.py b/solution/3400-3499/3436.Find Valid Emails/Solution.py new file mode 100644 index 0000000000000..e4b6ede1b6a1c --- /dev/null +++ b/solution/3400-3499/3436.Find Valid Emails/Solution.py @@ -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 diff --git a/solution/3400-3499/3436.Find Valid Emails/Solution.sql b/solution/3400-3499/3436.Find Valid Emails/Solution.sql new file mode 100644 index 0000000000000..6491214c4efea --- /dev/null +++ b/solution/3400-3499/3436.Find Valid Emails/Solution.sql @@ -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; diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 6e1e4e6035dcc..536b09b5aff39 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -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) | | 简单 | | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 3043b75616d6c..7e6fb265e927c 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -306,6 +306,7 @@ Press Control + F(or Command + F 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 diff --git a/solution/README.md b/solution/README.md index 3ebf76b794755..b6486d2e050a7 100644 --- a/solution/README.md +++ b/solution/README.md @@ -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) | | 简单 | | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 2e80bfa5e5510..e978b9a0dfc8e 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3444,6 +3444,7 @@ Press Control + F(or Command + F 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