PostgreSQL solution with detailed explanation and without `INITCAP`
IntuitionThe solution splits the text into words, capitalizes the first letter of each word (while making the rest lowercase), and handles hyphenated words by c
# Intuition
The solution splits the text into words, capitalizes the first letter of each word (while making the rest lowercase), and handles hyphenated words by capitalizing both parts. It then reassembles the words back into the original sentence with the correct formatting.

# Approach
`string_to_array`, `unnest`, `UPPER`, `LOWER`, `string_agg` (**Without** `INITCAP`)

# Complexity
- Time complexity:
`O(N∗M∗P)`

- Space complexity:
`O(N∗M)`




# Code
```postgresql []
WITH SplitWords AS (
    SELECT
        content_id,
        content_text AS original_text,
        string_agg(
            CASE
                -- Check if the word contains a hyphen
                WHEN word LIKE '%-%' THEN
                    -- Use a lateral join to split by hyphen and capitalize each part
                    (
                        SELECT string_agg(INITCAP(part), '-')
                        FROM unnest(string_to_array(word, '-')) AS part
                    )
                ELSE
                    -- For regular words, just capitalize the first letter
                    INITCAP(word)
            END, ' ') AS converted_text
    FROM (
        SELECT
            content_id,
            content_text,
            unnest(string_to_array(content_text, ' ')) AS word
        FROM user_content
    ) AS Words
    GROUP BY content_id, content_text
)
SELECT content_id, original_text, converted_text
FROM SplitWords
order by content_id

```

[Explanation is shared as a blogpost here](https://dev.to/subrata71/capitalizing-words-in-postgresql-handling-hyphenated-words-and-more-1l63)
['PostgreSQL']