Skip to content

Commit 3e957bc

Browse files
committed
Added task 3465
1 parent 9bfdaed commit 3e957bc

File tree

2 files changed

+75
-1
lines changed
  • src/main/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers

2 files changed

+75
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,8 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091-
| 3464 |[Maximize the Distance Between Points on a Square](src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square)| Hard | Array, Greedy, Binary_Search | 18 | 98.51
2091+
| 3465 |[Find Products with Valid Serial Numbers](src/main/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers)| Easy | Database | 292 | 90.91
2092+
| 3464 |[Maximize the Distance Between Points on a Square](src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square)| Hard | Array, Greedy, Binary_Search | 24 | 100.00
20922093
| 3463 |[Check If Digits Are Equal in String After Operations II](src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii)| Hard | String, Math, Number_Theory, Combinatorics | 38 | 100.00
20932094
| 3462 |[Maximum Sum With at Most K Elements](src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements)| Medium | Array, Sorting, Greedy, Matrix, Heap_(Priority_Queue) | 139 | 100.00
20942095
| 3461 |[Check If Digits Are Equal in String After Operations I](src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i)| Easy | String, Math, Simulation, Number_Theory, Combinatorics | 3 | 100.00
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3465\. Find Products with Valid Serial Numbers
5+
6+
Easy
7+
8+
Table: `products`
9+
10+
+--------------+------------+
11+
| Column Name | Type |
12+
+--------------+------------+
13+
| product_id | int |
14+
| product_name | varchar |
15+
| description | varchar |
16+
+--------------+------------+
17+
(product_id) is the unique key for this table.
18+
Each row in the table represents a product with its unique ID, name, and description.
19+
20+
Write a solution to find all products whose description **contains a valid serial number** pattern. A valid serial number follows these rules:
21+
22+
* It starts with the letters **SN** (case-sensitive).
23+
* Followed by exactly `4` digits.
24+
* It must have a hyphen (-) **followed by exactly** `4` digits.
25+
* The serial number must be within the description (it may not necessarily start at the beginning).
26+
27+
Return _the result table ordered by_ `product_id` _in **ascending** order_.
28+
29+
The result format is in the following example.
30+
31+
**Example:**
32+
33+
**Input:**
34+
35+
products table:
36+
37+
+------------+--------------+------------------------------------------------------+
38+
| product_id | product_name | description |
39+
+------------+--------------+------------------------------------------------------+
40+
| 1 | Widget A | This is a sample product with SN1234-5678 |
41+
| 2 | Widget B | A product with serial SN9876-1234 in the description |
42+
| 3 | Widget C | Product SN1234-56789 is available now |
43+
| 4 | Widget D | No serial number here |
44+
| 5 | Widget E | Check out SN4321-8765 in this description |
45+
+------------+--------------+------------------------------------------------------+
46+
47+
**Output:**
48+
49+
+------------+--------------+------------------------------------------------------+
50+
| product_id | product_name | description |
51+
+------------+--------------+------------------------------------------------------+
52+
| 1 | Widget A | This is a sample product with SN1234-5678 |
53+
| 2 | Widget B | A product with serial SN9876-1234 in the description |
54+
| 5 | Widget E | Check out SN4321-8765 in this description |
55+
+------------+--------------+------------------------------------------------------+
56+
57+
**Explanation:**
58+
59+
* **Product 1:** Valid serial number SN1234-5678
60+
* **Product 2:** Valid serial number SN9876-1234
61+
* **Product 3:** Invalid serial number SN1234-56789 (contains 5 digits after the hyphen)
62+
* **Product 4:** No serial number in the description
63+
* **Product 5:** Valid serial number SN4321-8765
64+
65+
The result table is ordered by product\_id in ascending order.
66+
67+
## Solution
68+
69+
```sql
70+
# Write your MySQL query statement below
71+
SELECT * FROM products WHERE description REGEXP 'SN[0-9]{4}-[0-9]{4}$'
72+
OR description REGEXP 'SN[0-9]{4}-[0-9]{4}[^0-9]+' ORDER BY product_id
73+
```

0 commit comments

Comments
 (0)