First Letter Capitalization Problem
Description
LeetCode Problem 3368.
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
Solution
Approach: Recursive CTE over the characters
MySQL has no built-in title-case function, and REGEXP_REPLACE cannot change the case of what it matches. So we rebuild each string one character at a time with a recursive CTE.
- The anchor row has
pos = 0and an emptyconvertedstring. - Each step looks at character
pos + 1. If it is the first character of the text, or the one before it (pos) is a space, it starts a word and is uppercased; otherwise it is lowercased. The character is appended toconverted. - The recursion stops when
posreachesCHAR_LENGTH(content_text), and the row with thatposholds the finished string.
Because every character, spaces included, is copied exactly once, the original spacing is preserved however many spaces there are. The recursion depth equals the text length, so it is bounded by MySQL's cte_max_recursion_depth (1000 by default), which is plenty for short texts. CAST('' AS CHAR(1000)) gives converted a wide enough type from the start; without it MySQL would size the column from the empty anchor string and truncate.
In Pandas, str.capitalize() already uppercases the first letter and lowercases the rest, so we split on single spaces (which keeps empty pieces, hence the spacing), capitalize each piece and join again.
WITH RECURSIVE walk AS (
-- pos = number of characters converted so far
SELECT content_id, content_text, 0 AS pos, CAST('' AS CHAR(1000)) AS converted
FROM user_content
UNION ALL
SELECT
content_id,
content_text,
pos + 1,
CONCAT(
converted,
-- a letter is a word start at the beginning or right after a space
IF(pos = 0 OR SUBSTRING(content_text, pos, 1) = ' ',
UPPER(SUBSTRING(content_text, pos + 1, 1)),
LOWER(SUBSTRING(content_text, pos + 1, 1)))
)
FROM walk
WHERE pos < CHAR_LENGTH(content_text)
)
SELECT content_id, content_text AS original_text, converted AS converted_text
FROM walk
WHERE pos = CHAR_LENGTH(content_text)
ORDER BY content_id;