Skip to main content

First Letter Capitalization II - Video Solutions

HardDatabase

LeetCode 3374: First Letter Capitalization II | Easy String Problem Explained Simply #leetcode #3374

InsightVanta
25:06869 views
3 video solutions available

First Letter Capitalization II - Video Solution

Watch 3 video solutions for First Letter Capitalization II, a hard level problem involving Database. This walkthrough by InsightVanta has 869 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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:

  • Convert the first letter of each word to uppercase and the remaining letters to lowercase
  • Special handling for words containing special characters:
    • For words connected with a hyphen -, both parts should be capitalized (e.g., top-rated → Top-Rated)
  • All other formatting and spacing should remain unchanged

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:

  • For content_id = 1:
    • Each word's first letter is capitalized: "Hello World Of Sql"
  • For content_id = 2:
    • Contains the hyphenated word "QUICK-brown" which becomes "Quick-Brown"
    • Other words follow normal capitalization rules
  • For content_id = 3:
    • Hyphenated word "modern-day" becomes "Modern-Day"
    • "DATA" is converted to "Data"
  • For content_id = 4:
    • Contains two hyphenated words: "web-based" → "Web-Based"
    • And "FRONT-end" → "Front-End"
Read full problem with examples

Approach Overview

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.

Complexity Analysis

ApproachTimeSpaceWhen to Use
String Manipulation with Regular ExpressionsO(n)O(n)When processing text in application code using Python or JavaScript
SQL Query with User Defined FunctionO(n)O(1)When the transformation must happen directly in the database layer