Watch 2 video solutions for First Letter Capitalization II, a hard level problem involving Database. This walkthrough by InsightVanta has 685 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
-, both parts should be capitalized (e.g., top-rated → Top-Rated)Return the result table that includes both the original content_text and the modified text following the above rules.
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 | modern-day DATA science | | 4 | web-based FRONT-end development | +------------+---------------------------------+
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 | modern-day DATA science | Modern-Day Data Science | | 4 | web-based FRONT-end development | Web-Based Front-End Development | +------------+---------------------------------+---------------------------------+
Explanation:
Problem Overview: You are given text stored in a database column and need to convert it so that the first character of every word is capitalized while the remaining characters stay lowercase. The transformation must handle word boundaries correctly and return the normalized string for each row.
Approach 1: String Manipulation with Regular Expressions (O(n) time, O(n) space)
This approach processes each string using a regular expression that identifies word boundaries and capitalizes the first character of each match. In Python or JavaScript, you scan the string once while applying a regex like \b[a-z] or similar patterns to capture the first letter of each word. The replacement function converts the captured character to uppercase while preserving the rest of the string. Since the regex engine walks through the input once, the time complexity is O(n) per string and the space complexity is O(n) due to the new transformed string.
This method is concise and easy to maintain. It is especially useful in application-level processing where you already fetch rows from the database and perform transformations using languages like Python or JavaScript. If you are comfortable with string manipulation and regular expressions, this approach provides a clean and expressive implementation.
Approach 2: SQL Query with User Defined Function (O(n) time, O(1) auxiliary space)
The database-centric approach performs capitalization directly inside SQL using a user defined function (UDF). The function iterates through characters of the input string and capitalizes letters that follow a word boundary such as a space or punctuation. Some SQL engines also support regex-based replacements, allowing expressions like REGEXP_REPLACE to convert the first letter of each word. Each row is processed independently, scanning the string once, giving O(n) time complexity per row.
This solution keeps the logic close to the data and avoids round trips to the application layer. It is often preferred when working heavily with database queries or when the transformation must be applied during reporting or ETL pipelines.
Recommended for interviews: The regex-based string manipulation approach is usually the clearest explanation during interviews because it demonstrates knowledge of word boundary detection and efficient linear-time processing. Mentioning the SQL UDF solution shows awareness of database-side transformations, which is valuable for backend or data engineering roles.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Manipulation with Regular Expressions | O(n) | O(n) | When processing text in application code using Python or JavaScript |
| SQL Query with User Defined Function | O(n) | O(1) | When the transformation must happen directly in the database layer |