Domain 1EasyCORE

Fix Product Name Format

The problem requires fixing the format of product names in a database table. Specifically, we need to ensure that: - Each product name starts with an uppercase letter. - All other letters in the product name are lowercase.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 0

1543. Fix Product Name Format

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 67.1%


Problem Breakdown

The problem requires fixing the format of product names in a database table. Specifically, we need to ensure that:

  • Each product name starts with an uppercase letter.
  • All other letters in the product name are lowercase.

Inputs:

  • A table named Products with at least one column ProductName.

Outputs:

  • The same table with the ProductName column updated to follow the specified format.

Implicit Constraints or Tricky Edge Cases:

  • Empty strings or null values in the ProductName column.
  • Product names that are already correctly formatted.
  • Product names with special characters or numbers, which should remain unchanged except for letter casing.

Intuition

The brute-force approach would involve iterating through each product name and manually adjusting the casing. This approach is straightforward but inefficient, especially if the table contains a large number of rows. The time complexity would be O(n * m), where n is the number of rows and m is the average length of the product names.

A better approach involves using SQL string functions to efficiently format the product names. By leveraging built-in functions like UPPER and LOWER, we can achieve the desired result with a single query, reducing both time and space complexity.

Approach

  1. Identify the SQL Functionality Needed:

    • Use SUBSTRING or LEFT to extract the first character of the product name.
    • Use UPPER to convert this character to uppercase.
    • Use LOWER to convert the rest of the product name to lowercase.
  2. Construct the Update Query:

    • Combine these functions to form a new string that follows the desired format.
    • Use an UPDATE statement to apply this transformation to all rows in the Products table.
  3. Handle Edge Cases:

    • Ensure that null or empty strings are handled gracefully, possibly by leaving them unchanged or setting them to a default value if necessary.

Complexity Analysis

  • Time Complexity: O(n), where n is the number of rows in the Products table. This is because we process each row exactly once.
  • Space Complexity: O(1), as we are updating the table in place and not using any additional data structures that grow with input size.

Solution

# SQL query to fix product name format
UPDATE Products
SET ProductName = UPPER(SUBSTRING(ProductName, 1, 1)) + LOWER(SUBSTRING(ProductName, 2))
WHERE ProductName IS NOT NULL AND ProductName <> '';

Edge Cases Handled

  • Null Values: The WHERE clause ensures that null values are not processed.
  • Empty Strings: The WHERE clause also skips empty strings.
  • Already Correctly Formatted Names: These will remain unchanged as the transformation logic only affects the casing of letters.