LeetCode 2129: Capitalize the Title (Length-Based Word Case Transform)
LeetCode 2129StringSimulationToday we solve LeetCode 2129 - Capitalize the Title.
Source: https://leetcode.com/problems/capitalize-the-title/
English
Problem Summary
Given a title string made of words separated by single spaces, transform each word by rule: if its length is at most 2, make all letters lowercase; otherwise capitalize only the first letter and lowercase the rest.
Key Insight
This is direct simulation. Split by spaces, normalize each word to lowercase first, then for words with length > 2 uppercase the first character. Join words back with spaces.
Algorithm
- Split title into words.
- For each word, convert to lowercase.
- If word length > 2, replace first char with uppercase version.
- Join processed words with a single space.
Complexity Analysis
Let n be total characters.
Time: O(n).
Space: O(n) for the output buffer/list.
Common Pitfalls
- Forgetting to lowercase long words before uppercasing first letter.
- Applying title-case to 1-2 letter words (should stay all lowercase).
- Losing spaces or introducing extra delimiters when rebuilding string.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public String capitalizeTitle(String title) {
String[] words = title.split(" ");
for (int i = 0; i < words.length; i++) {
String w = words[i].toLowerCase();
if (w.length() > 2) {
words[i] = Character.toUpperCase(w.charAt(0)) + w.substring(1);
} else {
words[i] = w;
}
}
return String.join(" ", words);
}
}func capitalizeTitle(title string) string {
words := strings.Split(title, " ")
for i, w := range words {
w = strings.ToLower(w)
if len(w) > 2 {
words[i] = strings.ToUpper(w[:1]) + w[1:]
} else {
words[i] = w
}
}
return strings.Join(words, " ")
}class Solution {
public:
string capitalizeTitle(string title) {
for (int i = 0; i < (int)title.size();) {
int j = i;
while (j < (int)title.size() && title[j] != ' ') {
title[j] = tolower(title[j]);
++j;
}
if (j - i > 2) title[i] = toupper(title[i]);
i = j + 1;
}
return title;
}
};class Solution:
def capitalizeTitle(self, title: str) -> str:
words = title.split(" ")
for i, w in enumerate(words):
w = w.lower()
if len(w) > 2:
words[i] = w[0].upper() + w[1:]
else:
words[i] = w
return " ".join(words)var capitalizeTitle = function(title) {
const words = title.split(' ');
for (let i = 0; i < words.length; i++) {
let w = words[i].toLowerCase();
if (w.length > 2) {
words[i] = w[0].toUpperCase() + w.slice(1);
} else {
words[i] = w;
}
}
return words.join(' ');
};中文
题目概述
给定一个由单空格分隔单词组成的标题字符串。转换规则:单词长度 ≤ 2 时全部小写;长度 > 2 时仅首字母大写,其余字母小写。
核心思路
这是标准模拟题。先按空格切词,对每个词先统一转小写,再根据长度决定是否把首字母转大写,最后按空格拼接回去即可。
算法步骤
- 按空格拆分为单词数组。
- 遍历每个单词并先转小写。
- 若长度 > 2,则仅将首字符转大写。
- 处理完成后用空格连接返回。
复杂度分析
设总字符数为 n。
时间复杂度:O(n)。
空间复杂度:O(n)(输出与中间数组)。
常见陷阱
- 忘记先转小写,导致长单词中后续字母保留原始大写。
- 错把长度 1 或 2 的单词处理成首字母大写。
- 重建字符串时空格处理不当,导致多空格或缺空格。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public String capitalizeTitle(String title) {
String[] words = title.split(" ");
for (int i = 0; i < words.length; i++) {
String w = words[i].toLowerCase();
if (w.length() > 2) {
words[i] = Character.toUpperCase(w.charAt(0)) + w.substring(1);
} else {
words[i] = w;
}
}
return String.join(" ", words);
}
}func capitalizeTitle(title string) string {
words := strings.Split(title, " ")
for i, w := range words {
w = strings.ToLower(w)
if len(w) > 2 {
words[i] = strings.ToUpper(w[:1]) + w[1:]
} else {
words[i] = w
}
}
return strings.Join(words, " ")
}class Solution {
public:
string capitalizeTitle(string title) {
for (int i = 0; i < (int)title.size();) {
int j = i;
while (j < (int)title.size() && title[j] != ' ') {
title[j] = tolower(title[j]);
++j;
}
if (j - i > 2) title[i] = toupper(title[i]);
i = j + 1;
}
return title;
}
};class Solution:
def capitalizeTitle(self, title: str) -> str:
words = title.split(" ")
for i, w in enumerate(words):
w = w.lower()
if len(w) > 2:
words[i] = w[0].upper() + w[1:]
else:
words[i] = w
return " ".join(words)var capitalizeTitle = function(title) {
const words = title.split(' ');
for (let i = 0; i < words.length; i++) {
let w = words[i].toLowerCase();
if (w.length > 2) {
words[i] = w[0].toUpperCase() + w.slice(1);
} else {
words[i] = w;
}
}
return words.join(' ');
};
Comments