Approach
Depth-first search
For First Letter Capitalization, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 47 lines of SQL from the credited upstream file 3368.sql.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1WITH RECURSIVE2 Words AS (3 4 SELECT5 content_id,6 SUBSTRING_INDEX(content_text, ' ', 1) AS word,7 SUBSTRING(8 content_text,9 LENGTH(SUBSTRING_INDEX(content_text, ' ', 1)) + 210 ) AS remaining_text,11 1 AS token_index12 FROM user_content13 UNION ALL14 15 SELECT16 content_id,17 SUBSTRING_INDEX(remaining_text, ' ', 1) AS word,18 SUBSTRING(19 remaining_text,20 LENGTH(SUBSTRING_INDEX(remaining_text, ' ', 1)) + 221 ) AS remaining_text,22 token_index + 1 AS token_index23 FROM Words24 WHERE remaining_text != ''25 ),26 Converted AS (27 28 SELECT29 content_id,30 GROUP_CONCAT(31 CONCAT(32 UPPER(SUBSTRING(word, 1, 1)),33 LOWER(SUBSTRING(word, 2))34 )35 ORDER BY token_index SEPARATOR ' '36 ) AS converted_text37 FROM Words38 GROUP BY 139 )40SELECT41 UserContent.content_id,42 UserContent.content_text AS original_text,43 Converted.converted_text44FROM user_content AS UserContent45INNER JOIN Converted46 USING (content_id);47