{"id":8637,"date":"2021-10-24T23:47:43","date_gmt":"2021-10-25T06:47:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8637"},"modified":"2021-10-24T23:55:01","modified_gmt":"2021-10-25T06:55:01","slug":"leetcode-2047-number-of-valid-words-in-a-sentence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2047-number-of-valid-words-in-a-sentence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2047. Number of Valid Words in a Sentence"},"content":{"rendered":"\n<p>A sentence consists of lowercase letters (<code>'a'<\/code>&nbsp;to&nbsp;<code>'z'<\/code>), digits (<code>'0'<\/code>&nbsp;to&nbsp;<code>'9'<\/code>), hyphens (<code>'-'<\/code>), punctuation marks (<code>'!'<\/code>,&nbsp;<code>'.'<\/code>, and&nbsp;<code>','<\/code>), and spaces (<code>' '<\/code>) only. Each sentence can be broken down into&nbsp;<strong>one or more tokens<\/strong>&nbsp;separated by one or more spaces&nbsp;<code>' '<\/code>.<\/p>\n\n\n\n<p>A token is a valid word if:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It only contains lowercase letters, hyphens, and\/or punctuation (<strong>no<\/strong>&nbsp;digits).<\/li><li>There is&nbsp;<strong>at most one<\/strong>&nbsp;hyphen&nbsp;<code>'-'<\/code>. If present, it should be surrounded by lowercase characters (<code>\"a-b\"<\/code>&nbsp;is valid, but&nbsp;<code>\"-ab\"<\/code>&nbsp;and&nbsp;<code>\"ab-\"<\/code>&nbsp;are not valid).<\/li><li>There is&nbsp;<strong>at most one<\/strong>&nbsp;punctuation mark. If present, it should be at the&nbsp;<strong>end<\/strong>&nbsp;of the token.<\/li><\/ul>\n\n\n\n<p>Examples of valid words include&nbsp;<code>\"a-b.\"<\/code>,&nbsp;<code>\"afad\"<\/code>,&nbsp;<code>\"ba-c\"<\/code>,&nbsp;<code>\"a!\"<\/code>, and&nbsp;<code>\"!\"<\/code>.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>sentence<\/code>, return&nbsp;<em>the&nbsp;<strong>number<\/strong>&nbsp;of valid words in&nbsp;<\/em><code>sentence<\/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> sentence = \"cat and  dog\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The valid words in the sentence are \"cat\", \"and\", and \"dog\".\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> sentence = \"!this  1-s b8d!\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no valid words in the sentence.\n\"!this\" is invalid because it starts with a punctuation mark.\n\"1-s\" and \"b8d\" are invalid because they contain digits.\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> sentence = \"alice and  bob are playing stone-game10\"\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The valid words in the sentence are \"alice\", \"and\", \"bob\", \"are\", and \"playing\".\n\"stone-game10\" is invalid because it contains digits.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> sentence = \"he bought 2 pencils, 3 erasers, and 1  pencil-sharpener.\"\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The valid words in the sentence are \"he\", \"bought\", \"pencils,\", \"erasers,\", \"and\", and \"pencil-sharpener.\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= sentence.length &lt;= 1000<\/code><\/li><li><code>sentence<\/code>&nbsp;only contains lowercase English letters, digits,&nbsp;<code>' '<\/code>,&nbsp;<code>'-'<\/code>,&nbsp;<code>'!'<\/code>,&nbsp;<code>'.'<\/code>, and&nbsp;<code>','<\/code>.<\/li><li>There will be at least&nbsp;<code>1<\/code>&nbsp;token.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countValidWords(string sentence) {\n    stringstream ss(sentence);\n    string word;\n    int ans = 0;\n    while (ss &gt;&gt; word) {      \n      bool valid = true;      \n      int hyphen = 0;\n      int punctuation = 0;\n      char p = ' ';\n      for (char c : word) {\n        if (c == '-') {          \n          if (++hyphen &gt; 1 || !isalpha(p)) {\n            valid = false;\n            break;\n          }\n        } else if (c == '!' || c == '.' || c == ',') {\n          if (++punctuation &gt; 1 || p == '-') {\n            valid = false;\n            break;\n          }\n        } else if (isalpha(c)) {          \n          if (punctuation) {\n            valid = false;\n            break;\n          }\n        } else {\n          valid = false;\n          break;\n        }\n        p = c;\n      }\n      if (word.back() == '-') \n        valid = false;\n      if (valid) ++ans;      \n    }\n    return ans;\n  }  \n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Regex<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)?<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def countValidWords(self, sentence: str) -> int:\n    ans = 0\n    for word in sentence.split():\n      if word.strip() and re.fullmatch('^([a-z]+(-?[a-z]+)?)?[\\.,!]?$', word.strip()):\n        ans += 1\n    return ans\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A sentence consists of lowercase letters (&#8216;a&#8217;&nbsp;to&nbsp;&#8216;z&#8217;), digits (&#8216;0&#8217;&nbsp;to&nbsp;&#8216;9&#8217;), hyphens (&#8216;-&#8216;), punctuation marks (&#8216;!&#8217;,&nbsp;&#8216;.&#8217;, and&nbsp;&#8216;,&#8217;), and spaces (&#8216; &#8216;) only. Each sentence can be broken&#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,564,224,4],"class_list":["post-8637","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-match","tag-regex","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8637","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=8637"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8637\/revisions"}],"predecessor-version":[{"id":8639,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8637\/revisions\/8639"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}