Fix Product Name Format — LeetCode 1543 Python Solution
EasyLeetCode PremiumDatabase
- Problem
- #1543
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
SQL
Table: Sales +--------------+---------+ | Column Name | Type | +--------------+---------+ | sale_id | int | | product_name | varchar | | sale_date | date | +--------------+---------+ sale_id is the column with unique values for this table. Each row of this table contains the product name and the date it was sold.Example
SQL
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| sale_id | int |
| product_name | varchar |
| sale_date | date |
+--------------+---------+
sale_id is the column with unique values for this table.
Each row of this table contains the product name and the date it was sold.Python solution
Python
import duckdb
import pandas as pd
def solution(sales: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Sales", sales)
return con.execute("""WITH
t AS (
SELECT
LOWER(TRIM(product_name)) AS product_name,
DATE_FORMAT(sale_date, '%Y-%m') AS sale_date
FROM Sales
)
SELECT product_name, sale_date, COUNT(1) AS total
FROM t
GROUP BY 1, 2
ORDER BY 1, 2;""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1543. Fix Product Name Format?
- LeetCode 1543. Fix Product Name Format is rated Easy on LeetCode.
- What topics does LeetCode 1543. Fix Product Name Format cover?
- LeetCode 1543. Fix Product Name Format is tagged Database on LeetCode.
- Is LeetCode 1543. Fix Product Name Format a premium problem?
- Yes. LeetCode 1543. Fix Product Name Format is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.