Difficulty: Easy | Acceptance: 60.60% | Paid: No Topics: Database
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 English letters and spaces.
Write a solution to fix the names so that only the first character is uppercase and the rest are lowercase.
The result format is in the following example.
- Examples
- Constraints
- Approach 1: Iterative Character Processing
- Approach 2: Split, Transform, and Join
Examples
Example 1:
Input:
Users table:
+---------+-------+
| user_id | name |
+---------+-------+
| 1 | Linda |
| 2 | John |
| 3 | alice |
| 4 | bOB |
| 5 | susan lauren |
+---------+-------+
Output:
+---------+-------+
| user_id | name |
+---------+-------+
| 1 | Linda |
| 2 | John |
| 3 | Alice |
| 4 | Bob |
| 5 | Susan Lauren |
+---------+-------+
Constraints
1 <= user_id <= 100
1 <= name.length <= 100
name consists of only English letters and spaces.
Approach 1: Iterative Character Processing
Intuition We iterate through each character of the name string. We maintain a flag to track if the current character is the start of a word (either the first character of the string or follows a space). If it is the start of a word, we convert it to uppercase; otherwise, we convert it to lowercase.
Steps
- Iterate through each row in the input data.
- For each name, initialize an empty result string and a
capitalizeNextflag set to true. - Loop through each character in the name:
- If the character is a space, append it to the result and set
capitalizeNextto true. - Otherwise, if
capitalizeNextis true, append the uppercase version of the character and set the flag to false. - If
capitalizeNextis false, append the lowercase version of the character.
- If the character is a space, append it to the result and set
- Return the processed data.
from typing import List
class Solution:
def fixNames(self, users: List[List[str]]) -> List[List[str]]:
result = []
for row in users:
user_id = row[0]
name = row[1]
fixed_name = []
capitalize_next = True
for char in name:
if char == ' ':
fixed_name.append(' ')
capitalize_next = True
else:
if capitalize_next:
fixed_name.append(char.upper())
capitalize_next = False
else:
fixed_name.append(char.lower())
result.append([user_id, ''.join(fixed_name)])
return result
Complexity
- Time: O(N * L) where N is the number of users and L is the max length of a name.
- Space: O(N * L) to store the result.
- Notes: This approach handles multiple spaces correctly by preserving them.
Approach 2: Split, Transform, and Join
Intuition We split the name string into an array of words based on spaces. We then iterate through each word, capitalizing the first character and lowercasing the rest. Finally, we join the words back into a single string with spaces.
Steps
- Iterate through each row in the input data.
- Split the name string by spaces.
- For each word in the split array:
- If the word is not empty, convert the first character to uppercase and the remaining characters to lowercase.
- Join the processed words back into a string with a space separator.
- Return the processed data.
from typing import List
class Solution:
def fixNames(self, users: List[List[str]]) -> List[List[str]]:
result = []
for row in users:
user_id = row[0]
name = row[1]
# Split, capitalize each part, and join
# Note: str.capitalize() lowercases the rest, but makes the rest lowercase.
# However, standard str.capitalize() also lowercases the rest of the string.
# We need to ensure only the first letter is upper, rest lower.
# Python's str.title() works well for letters and spaces.
fixed_name = name.title()
result.append([user_id, fixed_name])
return result
Complexity
- Time: O(N * L) where N is the number of users and L is the max length of a name.
- Space: O(N * L) to store the result and the split words array.
- Notes: This approach is concise and leverages built-in string manipulation functions. Note that simple split/join implementations may collapse multiple consecutive spaces.