Problem Description
You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters. Capitalize the string by changing the capitalization of each word such that:
- If the length of the word is 1 or 2 letters, change all letters to lowercase.
- Otherwise, change the first letter to uppercase and the remaining letters to lowercase.
Return the capitalized title.
Key Insights
- Each word's capitalization depends on its length.
- Words with length 1 or 2 should be entirely in lowercase.
- For longer words, only the first letter should be uppercase while the rest should be lowercase.
- The input string does not contain leading or trailing spaces and consists of words separated by a single space.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the input string, as we need to iterate through each character. Space Complexity: O(n), for storing the result string.
Solution
To solve the problem, we can follow these steps:
- Split the input string into individual words using space as a delimiter.
- For each word, check its length:
- If the length is 1 or 2, convert it to lowercase.
- If the length is 3 or more, capitalize the first letter and convert the rest to lowercase.
- Join the transformed words back into a single string with spaces in between.
- Return the final capitalized string.
This approach utilizes a list to store the modified words and string manipulation functions to achieve the desired capitalization.