Capitalize the Title — Solution in Python, Java & C++
Capitalize the Title solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings.
Capitalize the Title is an easy coding problem on strings, asked in Amazon and Adobe interviews.
- Difficulty: Easy
- Topics: Strings
- Asked at: Amazon, Adobe
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given a string title of words separated by single spaces. Capitalise it as follows:
- Words of length 1 or 2 become entirely lowercase.
- Longer words become lowercase except for their first letter, which is uppercased.
Return the capitalised title.
Example 1
Input: title = "capiTalIze tHe titLe"
Output: Capitalize The Title
Example 2
Input: title = "First leTTeR of EACH Word"
Output: First Letter of Each Word
Explanation: "of" has length 2, so it stays lowercase.
Example 3
Input: title = "i lOve leetcode"
Output: i Love Leetcode
Constraints
1 <= title.length <= 100title consists of English letters and single spaces, with no leading or trailing spaces.
Hints for Capitalize the Title
Hint 1
Handle each word independently, then re-join with single spaces.
Hint 2
Lowercase the whole word first — that fixes any stray capitals in the middle.
Hint 3
Only then decide whether to uppercase the first letter, based on the word's length.
How to solve Capitalize the Title
Normalise first, then decide: lowercasing the whole word removes the interior capitals, after which the length rule alone determines the first letter.
Approach
- Split on single spaces.
- Lowercase each word.
- If the word is longer than 2 characters, uppercase its first letter.
- Join with single spaces.
Why it works
Lowercasing first means the rule for the first letter is independent of the input's original casing, which is what makes both example transformations agree.
Complexity
- Time —
O(n) - Space —
O(n)
Pitfalls
- Uppercasing the first letter without lowercasing the rest leaves
"tHe"as"THe". - The threshold is
> 2, so a two-letter word like"of"stays lowercase while a three-letter one like"the"is capitalised.
Reference solution
Python
def capitalizeTitle(title: str) -> str:
out = []
for w in title.split(" "):
lower = w.lower()
out.append(lower if len(lower) <= 2 else lower[0].upper() + lower[1:])
return " ".join(out)Java
public static String capitalizeTitle(String title) {
String[] words = title.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < words.length; i++) {
if (i > 0) sb.append(' ');
String lower = words[i].toLowerCase();
if (lower.length() <= 2) sb.append(lower);
else sb.append(Character.toUpperCase(lower.charAt(0))).append(lower.substring(1));
}
return sb.toString();
}C++
string capitalizeTitle(string title) {
vector<string> words;
string cur;
for (char c : title) {
if (c == ' ') { words.push_back(cur); cur.clear(); }
else cur += c;
}
words.push_back(cur);
string res;
for (int i = 0; i < (int) words.size(); i++) {
string w = words[i];
for (auto& ch : w) {
if (ch >= 'A' && ch <= 'Z') ch = (char) (ch + 32);
}
if (w.size() > 2 && w[0] >= 'a' && w[0] <= 'z') w[0] = (char) (w[0] - 32);
if (i > 0) res += ' ';
res += w;
}
return res;
}JavaScript
var capitalizeTitle = function(title) {
const words = title.split(" ");
const out = [];
for (let i = 0; i < words.length; i++) {
const lower = words[i].toLowerCase();
if (lower.length <= 2) out.push(lower);
else out.push(lower.charAt(0).toUpperCase() + lower.slice(1));
}
return out.join(" ");
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings