Class Solution
java.lang.Object
g2001_2100.s2047_number_of_valid_words_in_a_sentence.Solution
2047 - Number of Valid Words in a Sentence.<p>Easy</p>
<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>
<p>A token is a valid word if <strong>all three</strong> of the following are true:</p>
<ul>
<li>It only contains lowercase letters, hyphens, and/or punctuation ( <strong>no</strong> digits).</li>
<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>
<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>
</ul>
<p>Examples of valid words include <code>"a-b."</code>, <code>"afad"</code>, <code>"ba-c"</code>, <code>"a!"</code>, and <code>"!"</code>.</p>
<p>Given a string <code>sentence</code>, return <em>the <strong>number</strong> of valid words in</em> <code>sentence</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> sentence = “cat and dog”</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The valid words in the sentence are “cat”, “and”, and “dog”.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> sentence = “!this 1-s b8d!”</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> There are no valid words in the sentence.</p>
<p>“!this” is invalid because it starts with a punctuation mark.</p>
<p>“1-s” and “b8d” are invalid because they contain digits.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> sentence = “alice and bob are playing stone-game10”</p>
<p><strong>Output:</strong> 5</p>
<p><strong>Explanation:</strong> The valid words in the sentence are “alice”, “and”, “bob”, “are”, and “playing”. “stone-game10” is invalid because it contains digits.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= sentence.length <= 1000</code></li>
<li><code>sentence</code> only contains lowercase English letters, digits, <code>' '</code>, <code>'-'</code>, <code>'!'</code>, <code>'.'</code>, and <code>','</code>.</li>
<li>There will be at least <code>1</code> token.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
countValidWords
-