Table: user_content
+-------------+---------+ | Column Name | Type | +-------------+---------+ | content_id | int | | content_text| varchar | +-------------+---------+ content_id is the unique key for this table. Each row contains a unique ID and the corresponding text content.
Write a solution to transform the text in the content_text column by applying the following rules:
Note: There will be no special character in content_text.
Return the result table that includes both the original content_text and the modified text where each word starts with a capital letter.
The result format is in the following example.
Example:
Input:
user_content table:
+------------+-----------------------------------+ | content_id | content_text | +------------+-----------------------------------+ | 1 | hello world of SQL | | 2 | the QUICK brown fox | | 3 | data science AND machine learning | | 4 | TOP rated programming BOOKS | +------------+-----------------------------------+
Output:
+------------+-----------------------------------+-----------------------------------+ | content_id | original_text | converted_text | +------------+-----------------------------------+-----------------------------------+ | 1 | hello world of SQL | Hello World Of Sql | | 2 | the QUICK brown fox | The Quick Brown Fox | | 3 | data science AND machine learning | Data Science And Machine Learning | | 4 | TOP rated programming BOOKS | Top Rated Programming Books | +------------+-----------------------------------+-----------------------------------+
Explanation:
Problem Overview: The task is to transform text in a database column so the first letter is capitalized while the remaining characters stay lowercase. You need to apply this transformation across all rows using database-friendly string operations.
Approach 1: SQL String Manipulation (O(n * m) time, O(1) extra space)
In SQL, the standard approach uses built‑in string functions to split the first character from the rest of the string. Extract the first character with LEFT() or SUBSTRING(), convert it to uppercase using UPPER(), then concatenate it with the remaining substring converted using LOWER(). The transformation typically looks like CONCAT(UPPER(LEFT(col,1)), LOWER(SUBSTRING(col,2))). The database scans each row once and performs constant‑time string operations per character, resulting in O(n * m) time where n is the number of rows and m is the average string length. Space complexity remains O(1) since the transformation happens during query execution.
This approach fits naturally inside a SELECT statement and avoids procedural loops. It relies entirely on SQL primitives, which keeps the solution portable across relational systems that support common string functions. Problems like this frequently appear in database practice sets because they test familiarity with SQL transformations rather than algorithmic data structures.
Approach 2: Pandas Vectorized String Operations (O(n * m) time, O(n) space)
In a data analysis workflow, the same transformation can be applied using Pandas. Use vectorized string operations such as Series.str.capitalize() or combine str[0].str.upper() with str[1:].str.lower(). Pandas processes the entire column in a vectorized manner rather than looping through rows in Python, which keeps the runtime efficient. The operation touches every character once, giving O(n * m) time complexity.
This method is useful when the data is already loaded into a DataFrame for analysis or preprocessing. The extra memory required for the transformed column leads to O(n) auxiliary space. Conceptually, the operation is still a simple string transformation applied row‑wise.
Recommended for interviews: The SQL string manipulation approach is what interviewers expect in database rounds. It shows you understand how to combine UPPER, LOWER, LEFT, and SUBSTRING to reshape text directly in a query. The Pandas version demonstrates the same logic in data pipelines but is less common in pure SQL interviews.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| SQL String Manipulation with UPPER/LOWER | O(n * m) | O(1) | Best for database queries where text must be transformed directly in SQL |
| Pandas Vectorized String Operations | O(n * m) | O(n) | When processing the dataset inside a Python data pipeline or notebook |
Word Ladder - Breadth First Search - Leetcode 127 - Python • NeetCode • 145,415 views views
Watch 9 more video solutions →Practice First Letter Capitalization with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor