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:
1
or 2
letters, change all letters to lowercase.Return the capitalized title
.
Example 1:
Input: title = "capiTalIze tHe titLe" Output: "Capitalize The Title" Explanation: Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.
Example 2:
Input: title = "First leTTeR of EACH Word" Output: "First Letter of Each Word" Explanation: The word "of" has length 2, so it is all lowercase. The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
Example 3:
Input: title = "i lOve leetcode" Output: "i Love Leetcode" Explanation: The word "i" has length 1, so it is lowercase. The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
Constraints:
1 <= title.length <= 100
title
consists of words separated by a single space without any leading or trailing spaces.The problem requires capitalizing a title string according to specific rules: words with length 1 or 2 are converted to lowercase, while words with length 3 or more have their first letter capitalized and the rest lowercased.
The most efficient approach involves these steps:
Split the string: The input string title
is split into individual words using space as the delimiter. This can be done using built-in string functions like split()
in Python, split(" ")
in Java, or similar functions in other languages.
Process each word: Each word is then processed individually:
lower()
or toLowerCase()
. Otherwise, the first character is capitalized using upper()
or toUpperCase()
and the rest is converted to lowercase. Functions like capitalize()
or title()
can simplify this step in some languages.Join the words: The processed words are then joined back together using spaces to form the final capitalized title string. This can be done using join()
or similar methods.
Time Complexity: The algorithm iterates through the words of the input string once for splitting, and once more for processing each word. The string manipulation operations (lowercase, uppercase, etc.) typically take time proportional to the length of the word. Therefore, the overall time complexity is O(n), where n is the length of the input string title
.
Space Complexity: The space complexity is also O(n) because:
The provided code examples demonstrate the solution in various programming languages. Here's a breakdown of one example (Python) and the key differences in others:
Python:
class Solution:
def capitalizeTitle(self, title: str) -> str:
words = [w.lower() if len(w) < 3 else w.capitalize() for w in title.split()]
return " ".join(words)
title.split()
: Splits the title string into a list of words.[w.lower() if len(w) < 3 else w.capitalize() for w in ... ]
: This is a list comprehension. It efficiently iterates through the words, applying the lowercase or capitalize logic based on the word length. w.lower()
converts to lowercase, and w.capitalize()
capitalizes the first letter and lowercases the rest." ".join(words)
: Joins the processed words back into a string, separated by spaces.Java:
Java uses more explicit looping and conditional statements but follows the same logic:
class Solution {
public String capitalizeTitle(String title) {
List<String> ans = new ArrayList<>();
for (String s : title.split(" ")) { //Iterates through words
if (s.length() < 3) {
ans.add(s.toLowerCase());
} else {
ans.add(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
}
}
return String.join(" ", ans);
}
}
Other languages (C++, Go, TypeScript, C#) follow similar patterns, adapting the syntax to their specific string manipulation functions and data structures. The core algorithm remains consistent across all examples.