2129. Capitalize the Title LeetCode Solution
In this guide, you will get 2129. Capitalize the Title LeetCode Solution with the best time and space complexity. The solution to Capitalize the Title problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Capitalize the Title solution in C++
- Capitalize the Title solution in Java
- Capitalize the Title solution in Python
- Additional Resources
Problem Statement of Capitalize the Title
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 1 or 2 letters, 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 <= 100
title consists 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.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2129. Capitalize the Title LeetCode Solution in C++
class Solution {
public:
string capitalizeTitle(string title) {
ranges::transform(title, title.begin(), ::tolower);
int i = 0; // Point to the start of a word.
int j = 0; // Point to the end of a word.
while (j < title.length()) {
while (j < title.length() && title[j] != ' ')
++j;
if (j - i > 2)
title[i] = toupper(title[i]);
i = j + 1;
++j; // Skip the spaces.
}
return title;
}
};
/* code provided by PROGIEZ */
2129. Capitalize the Title LeetCode Solution in Java
class Solution {
public String capitalizeTitle(String title) {
StringBuilder sb = new StringBuilder(title.toLowerCase());
int i = 0; // Point to the start of a word.
int j = 0; // Point to the end of a word.
while (j < sb.length()) {
while (j < sb.length() && sb.charAt(j) != ' ')
++j;
if (j - i > 2)
sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
i = j + 1;
++j; // Skip the spaces.
}
return sb.toString();
}
}
// code provided by PROGIEZ
2129. Capitalize the Title LeetCode Solution in Python
class Solution:
def capitalizeTitle(self, title: str) -> str:
return ' '.join(s.lower() if len(s) < 3
else s.capitalize() for s in title.split())
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.