{"id":9411,"date":"2022-01-08T23:26:24","date_gmt":"2022-01-09T07:26:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9411"},"modified":"2022-01-08T23:30:08","modified_gmt":"2022-01-09T07:30:08","slug":"leetcode-2129-capitalize-the-title","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2129-capitalize-the-title\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2129. Capitalize the Title"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>title<\/code>&nbsp;consisting of one or more words separated by a single space, where each word consists of English letters.&nbsp;<strong>Capitalize<\/strong>&nbsp;the string by changing the capitalization of each word such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the length of the word is&nbsp;<code>1<\/code>&nbsp;or&nbsp;<code>2<\/code>&nbsp;letters, change all letters to lowercase.<\/li><li>Otherwise, change the first letter to uppercase and the remaining letters to lowercase.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>capitalized<\/strong>&nbsp;<\/em><code>title<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> title = \"capiTalIze tHe titLe\"\n<strong>Output:<\/strong> \"Capitalize The Title\"\n<strong>Explanation:<\/strong>\nSince all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> title = \"First leTTeR of EACH Word\"\n<strong>Output:<\/strong> \"First Letter of Each Word\"\n<strong>Explanation:<\/strong>\nThe word \"of\" has length 2, so it is all lowercase.\nThe 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.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> title = \"i lOve leetcode\"\n<strong>Output:<\/strong> \"i Love Leetcode\"\n<strong>Explanation:<\/strong>\nThe word \"i\" has length 1, so it is lowercase.\nThe 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.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= title.length &lt;= 100<\/code><\/li><li><code>title<\/code>&nbsp;consists of words separated by a single space without any leading or trailing spaces.<\/li><li>Each word consists of uppercase and lowercase English letters and is&nbsp;<strong>non-empty<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Straight forward<\/strong><\/h2>\n\n\n\n<p>Without splitting the sentence into words, we need to take care the word of length one and two.<\/p>\n\n\n\n<p>Tips: use std::tolower, std::toupper to transform letters.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string capitalizeTitle(string title) {\n    const int n = title.size();\n    for (int i = 0; i < title.size(); ++i) {\n      if ((i == 0 || title[i - 1] == ' ') \n          &#038;&#038; i + 2 < n &#038;&#038; title[i + 1] != ' ' &#038;&#038; title[i + 2] != ' ')\n        title[i] = toupper(title[i]);\n      else\n        title[i] = tolower(title[i]);         \n    }\n    return title;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def capitalizeTitle(self, title: str) -> str:\n    return \" \".join(map(lambda w: w.lower() if len(w) <= 2 else w[0].upper() + w[1:].lower(), title.split()))\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;title&nbsp;consisting of one or more words separated by a single space, where each word consists of English letters.&nbsp;Capitalize&nbsp;the string by changing&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,276,4,168],"class_list":["post-9411","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-split","tag-string","tag-words","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9411"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9411\/revisions"}],"predecessor-version":[{"id":9414,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9411\/revisions\/9414"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}