Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 <= 100title consists of words separated by a single space without any leading or trailing spaces.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"capiTalIze tHe titLe"
"First leTTeR of EACH Word"
"i lOve leetcode"
detect-capital)to-lower-case)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2129: Capitalize the Title
class Solution {
public String capitalizeTitle(String title) {
List<String> ans = new ArrayList<>();
for (String s : title.split(" ")) {
if (s.length() < 3) {
ans.add(s.toLowerCase());
} else {
ans.add(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
}
}
return String.join(" ", ans);
}
}
// Accepted solution for LeetCode #2129: Capitalize the Title
func capitalizeTitle(title string) string {
title = strings.ToLower(title)
words := strings.Split(title, " ")
for i, s := range words {
if len(s) > 2 {
words[i] = strings.Title(s)
}
}
return strings.Join(words, " ")
}
# Accepted solution for LeetCode #2129: Capitalize the Title
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)
// Accepted solution for LeetCode #2129: Capitalize the Title
struct Solution;
impl Solution {
fn capitalize_title(title: String) -> String {
let words: Vec<String> = title
.split_whitespace()
.map(Solution::capitalize_word)
.collect();
words.join(" ")
}
fn capitalize_word(s: &str) -> String {
let n = s.len();
let mut res = "".to_string();
for (i, c) in s.char_indices() {
if i == 0 {
if n <= 2 {
res.push(c.to_ascii_lowercase());
} else {
res.push(c.to_ascii_uppercase());
}
} else {
res.push(c.to_ascii_lowercase());
}
}
res
}
}
#[test]
fn test() {
let title = "capiTalIze tHe titLe".to_string();
let res = "Capitalize The Title".to_string();
assert_eq!(Solution::capitalize_title(title), res);
let title = "First leTTeR of EACH Word".to_string();
let res = "First Letter of Each Word".to_string();
assert_eq!(Solution::capitalize_title(title), res);
let title = "i lOve leetcode".to_string();
let res = "i Love Leetcode".to_string();
assert_eq!(Solution::capitalize_title(title), res);
}
// Accepted solution for LeetCode #2129: Capitalize the Title
function capitalizeTitle(title: string): string {
return title
.split(' ')
.map(s =>
s.length < 3 ? s.toLowerCase() : s.slice(0, 1).toUpperCase() + s.slice(1).toLowerCase(),
)
.join(' ');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.