跳至主要內容

2129. 将标题首字母大写


2129. 将标题首字母大写

🟢   🔖  字符串  🔗 力扣open in new window LeetCodeopen in new window

题目

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.

题目大意

给你一个字符串 title ,它由单个空格连接一个或多个单词组成,每个单词都只包含英文字母。请你按以下规则将每个单词的首字母 大写

  • 如果单词的长度为 1 或者 2 ,所有字母变成小写。
  • 否则,将单词首字母大写,剩余字母变成小写。

请你返回 大写后title

示例 1:

输入: title = "capiTalIze tHe titLe"

输出: "Capitalize The Title"

解释:

由于所有单词的长度都至少为 3 ,将每个单词首字母大写,剩余字母变为小写。

示例 2:

输入: title = "First leTTeR of EACH Word"

输出: "First Letter of Each Word"

解释:

单词 "of" 长度为 2 ,所以它保持完全小写。

其他单词长度都至少为 3 ,所以其他单词首字母大写,剩余字母小写。

示例 3:

输入: title = "i lOve leetcode"

输出: "i Love Leetcode"

解释:

单词 "i" 长度为 1 ,所以它保留小写。

其他单词长度都至少为 3 ,所以其他单词首字母大写,剩余字母小写。

提示:

  • 1 <= title.length <= 100
  • title 由单个空格隔开的单词组成,且不含有任何前导或后缀空格。
  • 每个单词由大写和小写英文字母组成,且都是 非空 的。

解题思路

  1. 分割字符串:

    • 使用 split(' ') 将字符串拆分为单词数组。
  2. 遍历单词数组:

    • 对每个单词:
      • 如果单词长度小于 3,使用 toLowerCase() 转换为小写。
      • 如果单词长度为 3 或以上,使用 toUpperCase() 将首字母大写,同时将其余部分使用 toLowerCase() 转换为小写。
  3. 合并字符串:

    • 使用 join(' ') 将格式化后的单词数组重新组合为一个字符串。

复杂度分析

  • 时间复杂度O(n),其中 n 是字符串的长度,分割字符串、遍历单词数组、以及最终拼接字符串,时间复杂度均为 O(n)
  • 空间复杂度O(n),存储单词数组和修改后的字符串,需要额外的 O(n) 空间。

代码

/**
 * @param {string} title
 * @return {string}
 */
var capitalizeTitle = function (title) {
	// 分割字符串为单词数组
	const arr = title.split(' ');
	arr.forEach((str, i) => {
		if (str.length < 3) {
			// 长度小于 3 转换为小写
			arr[i] = str.toLowerCase();
		} else {
			// 首字母大写,剩余部分小写
			arr[i] = str[0].toUpperCase() + str.slice(1).toLowerCase();
		}
	});
	// 合并单词为结果字符串
	return arr.join(' ');
};

相关题目

题号标题题解标签难度力扣
520检测大写字母[✓]字符串🟢🀄️open in new window 🔗open in new window
709转换成小写字母[✓]字符串🟢🀄️open in new window 🔗open in new window