💥 Beats 100% on memory, 97.64% on runtime [EXPLAINED]
IntuitionTransform text so that each word starts with a capital letter while keeping everything else lowercase, even if words are hyphenated. The tricky part is
# Intuition
Transform text so that each word starts with a capital letter while keeping everything else lowercase, even if words are hyphenated. The tricky part is handling hyphenated words correctly without breaking their structure.

# Approach
 I split the text into words while keeping their order using an array, then I capitalize each word separately, handling hyphenated words by splitting and capitalizing both parts before joining everything back into a single sentence.

# Complexity
- Time complexity:
O(n), since I go through each word once to process and rebuild the text.

- Space complexity:
O(n), because I store the words in an array before reconstructing the sentence.

# Code
```postgresql []
WITH formatted AS (
    SELECT 
        content_id, 
        content_text AS original_text,
        STRING_AGG(
            CASE 
                -- Handle hyphenated words separately
                WHEN word LIKE '%-%' THEN 
                    INITCAP(SPLIT_PART(word, '-', 1)) || '-' || INITCAP(SPLIT_PART(word, '-', 2))
                ELSE 
                    INITCAP(word) 
            END, ' ' ORDER BY position
        ) AS converted_text
    FROM (
        -- Split the text into words while keeping their positions
        SELECT 
            content_id, 
            content_text, 
            unnest(string_to_array(content_text, ' ')) AS word,
            generate_series(1, array_length(string_to_array(content_text, ' '), 1)) AS position
        FROM user_content
    ) subquery
    GROUP BY content_id, content_text
)
SELECT * FROM formatted;

```
['Database', 'PostgreSQL']