|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README_EN.md |
| 5 | +tags: |
| 6 | + - Database |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- problem:start --> |
| 10 | + |
| 11 | +# [3415. Find Products with Three Consecutive Digits 🔒](https://leetcode.com/problems/find-products-with-three-consecutive-digits) |
| 12 | + |
| 13 | +[中文文档](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README.md) |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Table: <code>Products</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++-------------+---------+ |
| 23 | +| Column Name | Type | |
| 24 | ++-------------+---------+ |
| 25 | +| product_id | int | |
| 26 | +| name | varchar | |
| 27 | ++-------------+---------+ |
| 28 | +product_id is the unique key for this table. |
| 29 | +Each row of this table contains the ID and name of a product. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p>Write a solution to find all <strong>products</strong> whose names contain a <strong>sequence of exactly three digits in a row</strong>. </p> |
| 33 | + |
| 34 | +<p>Return <em>the result table ordered by</em> <code>product_id</code> <em>in <strong>ascending</strong> order.</em></p> |
| 35 | + |
| 36 | +<p>The result format is in the following example.</p> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong class="example">Example:</strong></p> |
| 40 | + |
| 41 | +<div class="example-block"> |
| 42 | +<p><strong>Input:</strong></p> |
| 43 | + |
| 44 | +<p>products table:</p> |
| 45 | + |
| 46 | +<pre class="example-io"> |
| 47 | ++-------------+--------------------+ |
| 48 | +| product_id | name | |
| 49 | ++-------------+--------------------+ |
| 50 | +| 1 | ABC123XYZ | |
| 51 | +| 2 | A12B34C | |
| 52 | +| 3 | Product56789 | |
| 53 | +| 4 | NoDigitsHere | |
| 54 | +| 5 | 789Product | |
| 55 | +| 6 | Item003Description | |
| 56 | +| 7 | Product12X34 | |
| 57 | ++-------------+--------------------+ |
| 58 | +</pre> |
| 59 | + |
| 60 | +<p><strong>Output:</strong></p> |
| 61 | + |
| 62 | +<pre class="example-io"> |
| 63 | ++-------------+--------------------+ |
| 64 | +| product_id | name | |
| 65 | ++-------------+--------------------+ |
| 66 | +| 1 | ABC123XYZ | |
| 67 | +| 5 | 789Product | |
| 68 | +| 6 | Item003Description | |
| 69 | ++-------------+--------------------+ |
| 70 | +</pre> |
| 71 | + |
| 72 | +<p><strong>Explanation:</strong></p> |
| 73 | + |
| 74 | +<ul> |
| 75 | + <li>Product 1: ABC123XYZ contains the digits 123.</li> |
| 76 | + <li>Product 5: 789Product contains the digits 789.</li> |
| 77 | + <li>Product 6: Item003Description contains 003, which is exactly three digits.</li> |
| 78 | +</ul> |
| 79 | + |
| 80 | +<p><strong>Note:</strong></p> |
| 81 | + |
| 82 | +<ul> |
| 83 | + <li>Results are ordered by <code>product_id</code> in ascending order.</li> |
| 84 | + <li>Only products with exactly three consecutive digits in their names are included in the result.</li> |
| 85 | +</ul> |
| 86 | +</div> |
| 87 | + |
| 88 | +<!-- description:end --> |
| 89 | + |
| 90 | +## Solutions |
| 91 | + |
| 92 | +<!-- solution:start --> |
| 93 | + |
| 94 | +### Solution 1: Regex Matching |
| 95 | + |
| 96 | +We can use regular expressions to match product names that contain three consecutive digits. |
| 97 | + |
| 98 | +<!-- tabs:start --> |
| 99 | + |
| 100 | +#### MySQL |
| 101 | + |
| 102 | +```sql |
| 103 | +# Write your MySQL query statement below |
| 104 | +SELECT product_id, name |
| 105 | +FROM Products |
| 106 | +WHERE name REGEXP '(^|[^0-9])[0-9]{3}([^0-9]|$)' |
| 107 | +ORDER BY 1; |
| 108 | +``` |
| 109 | + |
| 110 | +#### Pandas |
| 111 | + |
| 112 | +```python |
| 113 | +import pandas as pd |
| 114 | + |
| 115 | + |
| 116 | +def find_products(products: pd.DataFrame) -> pd.DataFrame: |
| 117 | + filtered = products[ |
| 118 | + products["name"].str.contains(r"(^|[^0-9])[0-9]{3}([^0-9]|$)", regex=True) |
| 119 | + ] |
| 120 | + return filtered.sort_values(by="product_id") |
| 121 | +``` |
| 122 | + |
| 123 | +<!-- tabs:end --> |
| 124 | + |
| 125 | +<!-- solution:end --> |
| 126 | + |
| 127 | +<!-- problem:end --> |
0 commit comments