Capitalize the Title
EASYDescription
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
1or2letters, change all letters to lowercase. - Otherwise, change the first letter to uppercase and the remaining 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 <= 100titleconsists of words separated by a single space without any leading or trailing spaces.- Each word consists of uppercase and lowercase English letters and is non-empty.
Approaches
Checkout 2 different approaches to solve Capitalize the Title. Click on different approaches to view the approach and algorithm in detail.
Split, Process, and Join
This approach is straightforward and relies on high-level string functions. It involves splitting the title into words, processing each word individually based on the capitalization rules, and then joining them back into a single string.
Algorithm
- Split the
titlestring by spaces to get an array ofwords. - Initialize a
StringBuilderto construct the result. - Loop through each
wordin thewordsarray. - If
word.length() <= 2, append the lowercase version of thewordto theStringBuilder. - Otherwise, append the first character of the
wordin uppercase, followed by the rest of thewordin lowercase. - Append a space after each word, except the last one.
- Return the final string from the
StringBuilder.
The algorithm first splits the input title string into an array of words using the space character as a delimiter. It then iterates through this array. For each word, it checks its length. If the length is 2 or less, the entire word is converted to lowercase. If the length is 3 or more, the first letter is converted to uppercase and the remaining letters are converted to lowercase. A StringBuilder is used to efficiently build the final string by appending each processed word followed by a space (except for the last word).
class Solution {
public String capitalizeTitle(String title) {
String[] words = title.split(" ");
StringBuilder result = new StringBuilder();
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (word.length() <= 2) {
result.append(word.toLowerCase());
} else {
result.append(Character.toUpperCase(word.charAt(0)));
result.append(word.substring(1).toLowerCase());
}
if (i < words.length - 1) {
result.append(" ");
}
}
return result.toString();
}
}
Complexity Analysis
Pros and Cons
- Simple and easy to understand.
- Leverages standard library functions, leading to concise code.
- Less memory-efficient due to the creation of an intermediate array of strings.
- Involves creating multiple temporary string objects during processing (e.g., from
toLowerCase,substring), which can increase garbage collection overhead.
Code Solutions
Checking out 4 solutions in different languages for Capitalize the Title. Click on different languages to view the code.
public class Solution {
public string CapitalizeTitle(string title) {
List < string > ans = new List < string > ();
foreach(string s in title.Split(' ')) {
if (s.Length < 3) {
ans.Add(s.ToLower());
} else {
ans.Add(char.ToUpper(s[0]) + s.Substring(1).ToLower());
}
}
return string.Join(" ", ans);
}
}Video Solution
Watch the video walkthrough for Capitalize the Title
Similar Questions
5 related questions you might find useful
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.