First Letter Capitalization II Problem
Description
LeetCode Problem 3374.
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.
A word is a maximal non-empty sequence of characters that does not contain a space.
Write a solution to transform the text in the content_text column by applying the following rules to each word:
- If the word starts with a character that is not an English letter, leave the entire word unchanged.
- Otherwise, if the word consists of two or more non-empty parts of English letters connected by hyphens
-, convert the first letter of each part to uppercase and the remaining letters of each part to lowercase. For example,top-ratedbecomesTop-RatedandFR-ONT-endbecomesFr-Ont-End. - Otherwise, convert the first letter of the word to uppercase and all remaining English letters to lowercase. Any special characters remain unchanged.
All other formatting and spacing must 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:
- The hyphenated word "QUICK-brown" becomes "Quick-Brown".
- Other words follow the normal capitalization rules.
- For content_id = 3:
- The hyphenated word "modern-day" becomes "Modern-Day".
- "DATA" is converted to "Data".
- For content_id = 4:
- "web-based" becomes "Web-Based".
- "FRONT-end" becomes "Front-End".
Constraints:
content_textcontains only English letters, spaces, and the characters\,@,-,/,^, and,.
Solution
Approach: Recursive CTE that remembers how the current word is treated
The rules depend on the whole word, so each word is classified once, when it starts, and every character is converted according to that class. As in the previous problem, a recursive CTE rebuilds the text one character at a time. Besides pos (characters done) and converted it carries kind, the class of the word that contains the next character:
0, the word does not start with an English letter: copy its characters unchanged.1, the word is two or more letter-only parts joined by single hyphens, i.e. it matches^[A-Za-z]+(-[A-Za-z]+)+$: uppercase a letter at the start of the word or right after a-, lowercase the others.2, anything else: uppercase only the first letter and lowercase the rest. Special characters have no case, so they are unaffected.
The anchor row classifies the first word (SUBSTRING_INDEX(text, ' ', 1)). In the recursive step, when the character just copied is a space, the next word begins and is classified from SUBSTRING(text, pos + 2); otherwise kind is kept. Copying every character also keeps all spaces exactly as they were.
The classification is strict: a--b has an empty part and x-@y has a part that is not all letters, so both fall into class 2 (A--b, X-@y). The recursion depth equals the text length, bounded by cte_max_recursion_depth (1000 by default).
The Pandas version applies the same three rules to each word of text.split(" ") and joins the words with single spaces, which keeps the original spacing.
WITH RECURSIVE walk AS (
-- pos = characters converted so far; kind = how the word holding the next
-- character is treated: 0 unchanged, 1 hyphenated, 2 ordinary
SELECT
content_id,
content_text,
0 AS pos,
CAST('' AS CHAR(1000)) AS converted,
CASE
WHEN SUBSTRING_INDEX(content_text, ' ', 1) NOT REGEXP '^[A-Za-z]' THEN 0
WHEN SUBSTRING_INDEX(content_text, ' ', 1) REGEXP '^[A-Za-z]+(-[A-Za-z]+)+$' THEN 1
ELSE 2
END AS kind
FROM user_content
UNION ALL
SELECT
content_id,
content_text,
pos + 1,
CONCAT(
converted,
CASE kind
WHEN 0 THEN SUBSTRING(content_text, pos + 1, 1)
WHEN 1 THEN IF(pos = 0 OR SUBSTRING(content_text, pos, 1) IN (' ', '-'),
UPPER(SUBSTRING(content_text, pos + 1, 1)),
LOWER(SUBSTRING(content_text, pos + 1, 1)))
ELSE IF(pos = 0 OR SUBSTRING(content_text, pos, 1) = ' ',
UPPER(SUBSTRING(content_text, pos + 1, 1)),
LOWER(SUBSTRING(content_text, pos + 1, 1)))
END
),
-- after a space the next word starts: classify it, otherwise keep the kind
IF(SUBSTRING(content_text, pos + 1, 1) = ' ',
CASE
WHEN SUBSTRING_INDEX(SUBSTRING(content_text, pos + 2), ' ', 1) NOT REGEXP '^[A-Za-z]' THEN 0
WHEN SUBSTRING_INDEX(SUBSTRING(content_text, pos + 2), ' ', 1) REGEXP '^[A-Za-z]+(-[A-Za-z]+)+$' THEN 1
ELSE 2
END,
kind)
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;