Leetcode 2129: Capitalize the Title
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'.
Approach: The approach to solving this problem involves iterating through each word in the string, applying the capitalization rules based on the word's length, and then combining the words back into a single string.
Observations:
• The main task is to process each word individually and change its capitalization according to its length.
• We need to be mindful of how spaces are handled when splitting and rejoining the string.
Steps:
• First, split the string into words using spaces.
• Then, check each word's length to determine how to capitalize it.
• For words with length <= 2, convert them entirely to lowercase.
• For words with length > 2, capitalize the first letter and make the rest lowercase.
• Finally, join the words back together into a single string with spaces between them.
Empty Inputs:
• The problem statement guarantees that the input will not be empty, so this case does not need handling.
Large Inputs:
• Ensure that the solution handles strings with the maximum allowed length of 100 characters efficiently.
Special Values:
• Words of length 1 or 2 should remain lowercase, and words of length 3 or more should follow the capitalization rule.
Constraints:
• The solution must efficiently handle up to 100 characters in the input string.
string capitalizeTitle(string s) {
for (int i = 0, j = 0; i <= s.size(); ++i) {
if (i == s.size() || s[i] == ' ') {
if (i - j > 2)
s[j] = toupper(s[j]);
j = i + 1;
}
else
s[i] = tolower(s[i]);
}
return s;
}
1 : Function Definition
string capitalizeTitle(string s) {
This line defines the function 'capitalizeTitle' that takes a string 's' as input and returns the string with each word capitalized according to the specified rule.
2 : Loop Initialization
for (int i = 0, j = 0; i <= s.size(); ++i) {
This line initializes a for loop with two variables: 'i' (to iterate through each character of the string) and 'j' (to mark the beginning of each word). The loop continues until the end of the string.
3 : Condition Check
if (i == s.size() || s[i] == ' ') {
This line checks if the current character is the end of the string or a space, indicating the end of a word.
4 : Word Length Check
if (i - j > 2)
This line checks if the length of the current word (from 'j' to 'i') is greater than two characters. Only words longer than two characters will have their first letter capitalized.
5 : Capitalization
s[j] = toupper(s[j]);
This line capitalizes the first letter of the current word (i.e., the character at position 'j').
6 : Update Word Start
j = i + 1;
This line updates the value of 'j' to mark the start of the next word after the space or end of the string.
7 : Lowercase Else
else
This line handles the case where the current character is part of a word that is not at the start. The characters in these positions will be converted to lowercase.
8 : Lowercasing
s[i] = tolower(s[i]);
This line converts the current character at position 'i' to lowercase, ensuring that non-initial letters of words are always in lowercase.
9 : Return Statement
return s;
This line returns the modified string 's', with all words properly capitalized according to the rules.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n) because we iterate through each character in the string, where n is the length of the input string.
Best Case: O(1)
Worst Case: O(n)
Description: The space complexity is O(n) because we store the words in an array temporarily during processing, where n is the length of the input string.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus