|
| 1 | +<h2><a href="https://leetcode.com/problems/number-of-valid-words-in-a-sentence">2047. Number of Valid Words in a Sentence</a></h2><h3>Easy</h3><hr><p>A sentence consists of lowercase letters (<code>'a'</code> to <code>'z'</code>), digits (<code>'0'</code> to <code>'9'</code>), hyphens (<code>'-'</code>), punctuation marks (<code>'!'</code>, <code>'.'</code>, and <code>','</code>), and spaces (<code>' '</code>) only. Each sentence can be broken down into <strong>one or more tokens</strong> separated by one or more spaces <code>' '</code>.</p> |
| 2 | + |
| 3 | +<p>A token is a valid word if <strong>all three</strong> of the following are true:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>It only contains lowercase letters, hyphens, and/or punctuation (<strong>no</strong> digits).</li> |
| 7 | + <li>There is <strong>at most one</strong> hyphen <code>'-'</code>. If present, it <strong>must</strong> be surrounded by lowercase characters (<code>"a-b"</code> is valid, but <code>"-ab"</code> and <code>"ab-"</code> are not valid).</li> |
| 8 | + <li>There is <strong>at most one</strong> punctuation mark. If present, it <strong>must</strong> be at the <strong>end</strong> of the token (<code>"ab,"</code>, <code>"cd!"</code>, and <code>"."</code> are valid, but <code>"a!b"</code> and <code>"c.,"</code> are not valid).</li> |
| 9 | +</ul> |
| 10 | + |
| 11 | +<p>Examples of valid words include <code>"a-b."</code>, <code>"afad"</code>, <code>"ba-c"</code>, <code>"a!"</code>, and <code>"!"</code>.</p> |
| 12 | + |
| 13 | +<p>Given a string <code>sentence</code>, return <em>the <strong>number</strong> of valid words in </em><code>sentence</code>.</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong class="example">Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> sentence = "<u>cat</u> <u>and</u> <u>dog</u>" |
| 20 | +<strong>Output:</strong> 3 |
| 21 | +<strong>Explanation:</strong> The valid words in the sentence are "cat", "and", and "dog". |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong class="example">Example 2:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> sentence = "!this 1-s b8d!" |
| 28 | +<strong>Output:</strong> 0 |
| 29 | +<strong>Explanation:</strong> There are no valid words in the sentence. |
| 30 | +"!this" is invalid because it starts with a punctuation mark. |
| 31 | +"1-s" and "b8d" are invalid because they contain digits. |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong class="example">Example 3:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input:</strong> sentence = "<u>alice</u> <u>and</u> <u>bob</u> <u>are</u> <u>playing</u> stone-game10" |
| 38 | +<strong>Output:</strong> 5 |
| 39 | +<strong>Explanation:</strong> The valid words in the sentence are "alice", "and", "bob", "are", and "playing". |
| 40 | +"stone-game10" is invalid because it contains digits. |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>1 <= sentence.length <= 1000</code></li> |
| 48 | + <li><code>sentence</code> only contains lowercase English letters, digits, <code>' '</code>, <code>'-'</code>, <code>'!'</code>, <code>'.'</code>, and <code>','</code>.</li> |
| 49 | + <li>There will be at least <code>1</code> token.</li> |
| 50 | +</ul> |
0 commit comments