{"id":9261,"date":"2021-12-26T07:32:55","date_gmt":"2021-12-26T15:32:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9261"},"modified":"2021-12-26T07:33:15","modified_gmt":"2021-12-26T15:33:15","slug":"leetcode-2114-maximum-number-of-words-found-in-sentences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2114-maximum-number-of-words-found-in-sentences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2114. Maximum Number of Words Found in Sentences"},"content":{"rendered":"\n<p>A&nbsp;<strong>sentence<\/strong>&nbsp;is a list of&nbsp;<strong>words<\/strong>&nbsp;that are separated by a single space&nbsp;with no leading or trailing spaces.<\/p>\n\n\n\n<p>You are given an array of strings&nbsp;<code>sentences<\/code>, where each&nbsp;<code>sentences[i]<\/code>&nbsp;represents a single&nbsp;<strong>sentence<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum number of words<\/strong>&nbsp;that appear in a single sentence<\/em>.<\/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> sentences = [\"alice and bob love leetcode\", \"i think so too\", \"this is great thanks very much\"]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> \n- The first sentence, \"alice and bob love leetcode\", has 5 words in total.\n- The second sentence, \"i think so too\", has 4 words in total.\n- The third sentence, \"this is great thanks very much\", has 6 words in total.\nThus, the maximum number of words in a single sentence comes from the third sentence, which has 6 words.\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> sentences = [\"please wait\", \"continue to fight\", \"continue to win\"]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> It is possible that multiple sentences contain the same number of words. \nIn this example, the second and third sentences (underlined) have the same number of words.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= sentences.length &lt;= 100<\/code><\/li><li><code>1 &lt;= sentences[i].length &lt;= 100<\/code><\/li><li><code>sentences[i]<\/code>&nbsp;consists only of lowercase English letters and&nbsp;<code>' '<\/code>&nbsp;only.<\/li><li><code>sentences[i]<\/code>&nbsp;does not have leading or trailing spaces.<\/li><li>All the words in&nbsp;<code>sentences[i]<\/code>&nbsp;are separated by a single space.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Count spaces<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(sum(len(sentences[i]))<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  int mostWordsFound(vector<string>& sentences) {\n    int ans = 0;\n    for (const string& s : sentences)\n      ans = max(ans, 1 + static_cast<int>(count(begin(s), end(s), ' ')));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;sentence&nbsp;is a list of&nbsp;words&nbsp;that are separated by a single space&nbsp;with no leading or trailing spaces. You are given an array of strings&nbsp;sentences, where each&nbsp;sentences[i]&nbsp;represents a&#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,4],"class_list":["post-9261","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9261","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=9261"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9261\/revisions"}],"predecessor-version":[{"id":9263,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9261\/revisions\/9263"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}