#1667

Fix Names in a Table

Easy
DatabaseString ManipulationSQL Functions
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(1)
💡

Intuition

Time O(n)Space O(1)

Using SQL string functions efficiently allows us to transform the names in a single pass. This is much faster than the brute force method as we leverage built-in functions.

⚙️

Algorithm

3 steps
  1. 1Step 1: Use the SQL function CONCAT to combine the uppercase first character with the lowercase rest of the name.
  2. 2Step 2: Use the SUBSTRING function to extract the first character and the rest of the name.
  3. 3Step 3: Return the modified names ordered by user_id.
solution.py1 lines
1SELECT user_id, CONCAT(UPPER(SUBSTRING(name, 1, 1)), LOWER(SUBSTRING(name, 2))) AS name FROM Users ORDER BY user_id;

Complexity note: The time complexity is O(n) because we are processing each name in a single pass using SQL string functions, which are optimized for performance.

  • 1Using SQL string functions can optimize performance.
  • 2Understanding string manipulation is crucial for data formatting.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.