Problem Description
Given a string title consisting of one or more words separated by a single space, update the capitalization of each word based on its length. If a word’s length is 1 or 2, convert it entirely to lowercase. Otherwise, convert the first letter to uppercase and the remaining letters to lowercase. Return the modified string.
Key Insights
- Split the input string into words using the space as a delimiter.
- Process each word individually:
- For words with length 1 or 2, change to all lowercase.
- For longer words, capitalize the first character and convert the remaining characters to lowercase.
- Join the processed words back together with a space.
Space and Time Complexity
Time Complexity: O(n), where n is the number of characters in the title.
Space Complexity: O(n), for storing the split words and the final result.
Solution
The solution uses a simple iterative approach. First, the string is split into individual words. Each word is processed based on its length using string manipulation functions. For words longer than 2 characters, we modify just the first character to uppercase and ensure the rest are in lowercase. Finally, the words are concatenated back into a single string with spaces. This approach uses basic string operations and avoids extra data structures beyond what is necessary for processing the words.