Solution on PostgreSQL
Intuition\nThe contents of the strings must be converted according to the following conditions:\n- Convert the first letter of each word to uppercase and the re
# Intuition\nThe contents of the strings must be converted according to the following conditions:\n- Convert the first letter of each word to uppercase and the remaining letters to lowercase\n- Special handling for words containing special characters:\n    - For words connected with a hyphen -, both parts should be capitalized (e.g., top-rated \u2192 Top-Rated)\n- All other formatting and spacing should remain unchanged\n\n# Approach: using CTE\nThis part of the PostgreSQL query creates a Common table expressions named `converted_content` that contains three columns:\n`content_id`: The content ID.\n`original_text`: The original text of the content.\n`converted_text`: The converted text of the content - each first letter of the word is capitalized.\n\nThe view is created based on a query that first splits the original content text into individual words using the `string_to_array()` function. Then, for each word, it is checked to see if it contains a hyphen. If so, the phrase is split into its individual parts, which are then reassembled using a capital letter for each fragment and separated by a hyphen. If the word does not contain a hyphen, the word begins with a capital letter Finally, the converted text is reassembled into a string using the `STRING_AGG()` function.\n\n- CASE WHEN...THEN: The query checks to see if the `phrase` string contains the `-` character.\n- ARRAY_TO_STRING: If the string contains `-`, it is split into individual words (chunks) using the `string_to_array` function. Then the initials of each chunk are converted to uppercase using the `INITCAP` function, and the chunks are joined back into a string with a `-` separator using the `ARRAY_TO_STRING` function.\n- ELSE: If the word does not contain `-`, If the word does not contain a hyphen, the word begins with a capital letter using the `INITCAP` function.\n- AS converted_text: The result is stored in a new column named `converted_text`.\n\nThe subquery in the FROM clause of the query creates a new virtual table named phrases, which includes the following columns:\n\ncontent_id: Content identifier.\ncontent_text: Text content.\nphrase: An array of phrases extracted from the content_text field by splitting it on white space using the string_to_array function.\n\nThis subquery extracts data from the user_content table and converts the text content into an array of individual words (phrases). It then groups the results by the content_id and content_text fields, creating a new table that is used in the FROM clause of the main query.\n\n# Complexity\n- Time complexity:\nO(N*M)\n\n\n# Code\n```PostgreSQL []\nWITH converted_content AS (\n    SELECT \n        content_id,\n        content_text AS original_text,\n        STRING_AGG(\n            CASE \n                WHEN phrase LIKE \'%-%\' THEN\n                    ARRAY_TO_STRING(ARRAY(\n                        SELECT INITCAP(portion) \n                        FROM UNNEST(string_to_array(phrase, \'-\')) AS portion\n                    ), \'-\')\n                ELSE\n                    INITCAP(phrase)\n            END, \' \') AS converted_text\n    FROM (\n        SELECT \n            content_id,\n            content_text,\n            UNNEST(string_to_array(content_text, \' \')) AS phrase\n        FROM user_content\n    ) AS phrases\n    GROUP BY content_id, content_text\n)\nSELECT content_id, original_text, converted_text\nFROM converted_content\nORDER BY content_id;\n```\n\n# If you liked the solution, like it \uD83D\uDD3C
['Database', 'PostgreSQL']