返回介绍

solution / 2100-2199 / 2129.Capitalize the Title / README_EN

发布于 2024-06-17 01:03:09 字数 4928 浏览 0 评论 0 收藏 0

2129. Capitalize the Title

中文文档

Description

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.

Solutions

Solution 1: Simulation

Directly simulate the process. Split the string by spaces to get each word, then convert each word to the appropriate case as per the problem statement. Finally, join the words with spaces.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string 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)
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);
  }
}
class Solution {
public:
  string capitalizeTitle(string title) {
    transform(title.begin(), title.end(), title.begin(), ::tolower);
    istringstream ss(title);
    string ans;
    while (ss >> title) {
      if (title.size() > 2) {
        title[0] = toupper(title[0]);
      }
      ans += title;
      ans += " ";
    }
    ans.pop_back();
    return ans;
  }
};
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, " ")
}
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(' ');
}
public class Solution {
  public string CapitalizeTitle(string title) {
    List<string> ans = new List<string>();
    foreach (string s in title.Split(' ')) {
      if (s.Length < 3) {
        ans.Add(s.ToLower());
      } else {
        ans.Add(char.ToUpper(s[0]) + s.Substring(1).ToLower());
      }
    }
    return string.Join(" ", ans);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文