Skip to main content

First Letter Capitalization - Solution & Explanation

HardPremiumFree on FleetCodeDatabase4 min read
Practice this problem

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
  • Keep all other letters in lowercase
  • Preserve all existing spaces

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:

  • For content_id = 1:
    • Each word's first letter is capitalized: Hello World Of Sql
  • For content_id = 2:
    • Original mixed-case text is transformed to title case: The Quick Brown Fox
  • For content_id = 3:
    • The word AND is converted to "And": "Data Science And Machine Learning"
  • For content_id = 4:
    • Handles word TOP rated correctly: Top Rated
    • Converts BOOKS from all caps to title case: Books

Approach Overview

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.

Solution

Code

MySQL

Pandas

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
SQL String Manipulation with UPPER/LOWERO(n * m)O(1)Best for database queries where text must be transformed directly in SQL
Pandas Vectorized String OperationsO(n * m)O(n)When processing the dataset inside a Python data pipeline or notebook

Video Solution

Leetcode HARD 3368 - Recursive CTE Trick That Solves Hardest String Problem | SQL Tutorial • Everyday Data Science • 3,357 views views

Frequently Asked Questions

Is First Letter Capitalization easy or hard?
The problem is conceptually straightforward but appears in database tracks where SQL function knowledge matters. Once you know how to combine UPPER, LOWER, LEFT, and SUBSTRING, the query becomes simple and runs efficiently across large tables.
First Letter Capitalization Python/Java solution
In Python with Pandas, use Series.str.capitalize() to convert the first character to uppercase and the rest to lowercase across the entire column. In SQL environments such as MySQL, the equivalent uses CONCAT(UPPER(LEFT(col,1)), LOWER(SUBSTRING(col,2))). Both approaches run in O(n * m) time.
How to solve First Letter Capitalization in O(n)?
Treat each row independently and apply built-in string functions. Extract the first character with LEFT(column,1), convert it using UPPER(), convert the remainder with LOWER(SUBSTRING(column,2)), and combine them using CONCAT. The query performs a single pass over the table, effectively O(n) with respect to the number of rows.
What is the best approach for First Letter Capitalization?
The best approach uses SQL string functions such as UPPER(), LOWER(), LEFT(), and SUBSTRING() to transform the text directly in a SELECT query. This method scans each row once and constructs the result with CONCAT. The overall complexity is O(n * m), where n is the number of rows and m is the string length.
Is First Letter Capitalization asked at Google/Amazon/Meta?
String transformation tasks in SQL commonly appear in database interview rounds at companies like Amazon, Google, and Meta. While the exact problem may vary, the skill tested is the ability to combine SQL string functions to reshape text efficiently inside queries.
What data structure is used in First Letter Capitalization?
No advanced data structure is required. The solution relies on SQL string functions and row-wise processing within a database table. Each row is handled independently, making the problem more about query construction than algorithm design.
What is the time complexity of First Letter Capitalization?
The time complexity is O(n * m). The database processes n rows and performs character-level operations on strings of average length m. Since each character is touched once during case conversion, the complexity scales linearly with the dataset size.

Ready to solve this problem?

Practice First Letter Capitalization with our built-in code editor and test cases.

Practice on FleetCode