Leetcode 2129: Capitalize the Title

grid47
grid47
Exploring patterns and algorithms
Apr 8, 2024 6 min read

You are given a string consisting of multiple words separated by spaces. For each word, you need to change the capitalization based on the length of the word. If the word has 1 or 2 letters, it should be entirely in lowercase. If the word has 3 or more letters, the first letter should be uppercase and the remaining letters should be lowercase. Return the modified string with the updated capitalization for each word.
Problem
Approach
Steps
Complexity
Input: The input consists of a string where words are separated by a single space. Each word is a sequence of English letters with no leading or trailing spaces.
Example: title = 'hello World this IS an EXample'
Constraints:
• 1 <= title.length <= 100
• The string consists of words separated by a single space without any leading or trailing spaces.
• Each word contains only uppercase and lowercase English letters and is non-empty.
Output: Return the string with each word capitalized as per the given rules. Each word with a length of 1 or 2 letters will be lowercase, and the first letter of words with a length of 3 or more letters will be uppercase with the rest in lowercase.
Example: Input: title = 'hello World this IS an EXample' Output: 'Hello World This Is an Example'
Constraints:
• Ensure that the title is processed correctly for each word based on its length.
Goal: To modify the capitalization of each word based on its length, we need to iterate through the string and apply the capitalization rules accordingly.
Steps:
• Split the string into individual words.
• For each word, check its length.
• If the length is 1 or 2, convert all letters to lowercase.
• If the length is 3 or more, capitalize the first letter and convert the rest to lowercase.
• Join the words back together with a single space separating them.
• Return the resulting string.
Goal: The solution must handle strings of various lengths and must adhere to the given word capitalization rules.
Steps:
• Words must be processed correctly based on their length.
• The solution must handle strings with varying lengths up to the constraint limit.
Assumptions:
• The string will always contain at least one word.
• Words are separated by exactly one space and no extra spaces exist.
Input: Input: title = 'hello World this IS an EXample'
Explanation: The string contains five words. After processing each word based on its length, the result is 'Hello World This Is an Example'. Words with length 3 or more have their first letter capitalized, while shorter words are entirely in lowercase.

Input: Input: title = 'a quick Brown fox'
Explanation: Here, 'a' is a word with length 1 and is kept lowercase. 'quick' and 'Brown' have 3 or more letters, so their first letters are capitalized, and 'fox' is similarly processed. The result is 'a Quick Brown Fox'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus