3465. Find Products with Valid Serial Numbers LeetCode Solution

In this guide, you will get 3465. Find Products with Valid Serial Numbers LeetCode Solution with the best time and space complexity. The solution to Find Products with Valid Serial Numbers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Products with Valid Serial Numbers solution in C++
  4. Find Products with Valid Serial Numbers solution in Java
  5. Find Products with Valid Serial Numbers solution in Python
  6. Additional Resources
3465. Find Products with Valid Serial Numbers LeetCode Solution image

Problem Statement of Find Products with Valid Serial Numbers

Table: products

+————–+————+
| Column Name | Type |
+————–+————+
| product_id | int |
| product_name | varchar |
| description | varchar |
+————–+————+
(product_id) is the unique key for this table.
Each row in the table represents a product with its unique ID, name, and description.

Write a solution to find all products whose description contains a valid serial number pattern. A valid serial number follows these rules:

It starts with the letters SN (case-sensitive).
Followed by exactly 4 digits.
It must have a hyphen (-) followed by exactly 4 digits.
The serial number must be within the description (it may not necessarily start at the beginning).

Return the result table ordered by product_id in ascending order.
The result format is in the following example.

Example:

Input:
products table:

+————+————–+——————————————————+
| product_id | product_name | description |
+————+————–+——————————————————+
| 1 | Widget A | This is a sample product with SN1234-5678 |
| 2 | Widget B | A product with serial SN9876-1234 in the description |
| 3 | Widget C | Product SN1234-56789 is available now |
| 4 | Widget D | No serial number here |
| 5 | Widget E | Check out SN4321-8765 in this description |
+————+————–+——————————————————+

See also  1915. Number of Wonderful Substrings LeetCode Solution

Output:

+————+————–+——————————————————+
| product_id | product_name | description |
+————+————–+——————————————————+
| 1 | Widget A | This is a sample product with SN1234-5678 |
| 2 | Widget B | A product with serial SN9876-1234 in the description |
| 5 | Widget E | Check out SN4321-8765 in this description |
+————+————–+——————————————————+

Explanation:

Product 1: Valid serial number SN1234-5678
Product 2: Valid serial number SN9876-1234
Product 3: Invalid serial number SN1234-56789 (contains 5 digits after the hyphen)
Product 4: No serial number in the description
Product 5: Valid serial number SN4321-8765

The result table is ordered by product_id in ascending order.

Example not found

Constraints not found

Complexity Analysis

  • Time Complexity: Google AdSense
  • Space Complexity: Google Analytics

3465. Find Products with Valid Serial Numbers LeetCode Solution in C++

SELECT product_id, product_name, description
FROM Products
WHERE
  description REGEXP 'SN[0-9]{4}-[0-9]{4}$'
  OR description REGEXP 'SN[0-9]{4}-[0-9]{4}[^0-9]+'
ORDER BY 1;
/* code provided by PROGIEZ */

3465. Find Products with Valid Serial Numbers LeetCode Solution in Java

N/A
// code provided by PROGIEZ

3465. Find Products with Valid Serial Numbers LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.