Watch 10 video solutions for Fix Names in a Table, a easy level problem involving Database. This walkthrough by Learn With Chirag has 5,283 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Users
+----------------+---------+ | Column Name | Type | +----------------+---------+ | user_id | int | | name | varchar | +----------------+---------+ user_id is the primary key (column with unique values) for this table. This table contains the ID and the name of the user. The name consists of only lowercase and uppercase characters.
Write a solution to fix the names so that only the first character is uppercase and the rest are lowercase.
Return the result table ordered by user_id.
The result format is in the following example.
Example 1:
Input: Users table: +---------+-------+ | user_id | name | +---------+-------+ | 1 | aLice | | 2 | bOB | +---------+-------+ Output: +---------+-------+ | user_id | name | +---------+-------+ | 1 | Alice | | 2 | Bob | +---------+-------+
Problem Overview: The table contains user names with inconsistent capitalization. The task is to normalize each name so the first character is uppercase and all remaining characters are lowercase, then return the result ordered by user_id. This is primarily a string normalization problem often solved with built-in string functions in database queries or basic iteration in programming languages.
Approach 1: Use Loop to Transform Each Name (Time: O(n · m), Space: O(m))
Iterate through each name and rebuild it with the correct capitalization. Extract the first character, convert it using toUpperCase() or an equivalent function, and convert the remaining substring using toLowerCase(). Concatenate the two parts to form the normalized name. Here, n is the number of rows and m is the average length of a name. This approach mirrors how you would solve the problem in languages like C++, Java, Python, or JavaScript by iterating through strings and applying simple string operations. In SQL-based solutions, the same idea appears as UPPER(SUBSTRING(name,1,1)) combined with LOWER(SUBSTRING(name,2)).
The key insight is that only the first character needs special handling. Once separated, the remaining substring can be normalized with a single lowercase transformation. The algorithm scans each character at most once per name, making it efficient for large tables.
Approach 2: Use Regex Replacement Method (Time: O(n · m), Space: O(m))
Regular expressions provide a compact way to normalize names. A regex pattern captures the first character separately from the rest of the string. The replacement function then uppercases the first capture group and lowercases the second. Languages like Python and JavaScript support regex callbacks, allowing you to transform matched groups dynamically. This approach still processes each character once, so the time complexity remains proportional to the total length of all names.
Regex solutions are concise and expressive when you already rely on regex utilities in your codebase. However, they introduce slightly more overhead and are sometimes harder to read compared to direct substring manipulation.
Recommended for interviews: The loop or substring-based transformation is the expected solution. It clearly shows you understand string manipulation and built-in case conversion functions. Regex works as an alternative but is rarely necessary unless the formatting rules become more complex.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Loop to Transform Each Name | O(n · m) | O(m) | General case. Clear and readable solution using substring and case conversion functions. |
| Regex Replacement Method | O(n · m) | O(m) | Useful when regex utilities are already used or when string formatting rules become more complex. |