java.lang.Object
g2001_2100.s2047_number_of_valid_words_in_a_sentence.Solution

public class Solution extends Object
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>&quot;a-b&quot;</code> is valid, but <code>&quot;-ab&quot;</code> and <code>&quot;ab-&quot;</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>&quot;ab,&quot;</code>, <code>&quot;cd!&quot;</code>, and <code>&quot;.&quot;</code> are valid, but <code>&quot;a!b&quot;</code> and <code>&quot;c.,&quot;</code> are not valid).</li> </ul> <p>Examples of valid words include <code>&quot;a-b.&quot;</code>, <code>&quot;afad&quot;</code>, <code>&quot;ba-c&quot;</code>, <code>&quot;a!&quot;</code>, and <code>&quot;!&quot;</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 = &ldquo;cat and dog&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The valid words in the sentence are &ldquo;cat&rdquo;, &ldquo;and&rdquo;, and &ldquo;dog&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;!this 1-s b8d!&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> There are no valid words in the sentence.</p> <p>&ldquo;!this&rdquo; is invalid because it starts with a punctuation mark.</p> <p>&ldquo;1-s&rdquo; and &ldquo;b8d&rdquo; are invalid because they contain digits.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;alice and bob are playing stone-game10&rdquo;</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> The valid words in the sentence are &ldquo;alice&rdquo;, &ldquo;and&rdquo;, &ldquo;bob&rdquo;, &ldquo;are&rdquo;, and &ldquo;playing&rdquo;. &ldquo;stone-game10&rdquo; 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 Details

    • Solution

      public Solution()
  • Method Details

    • countValidWords

      public int countValidWords(String sentence)