A sentence consists of lowercase letters ('a' to 'z'), digits ('0' to '9'), hyphens ('-'), punctuation marks ('!', '.', and ','), and spaces (' ') only. Each sentence can be broken down into one or more tokens separated by one or more spaces ' '.

A token is a valid word if all three of the following are true:


	It only contains lowercase letters, hyphens, and/or punctuation (no digits).
	There is at most one hyphen '-'. If present, it must be surrounded by lowercase characters ("a-b" is valid, but "-ab" and "ab-" are not valid).
	There is at most one punctuation mark. If present, it must be at the end of the token ("ab,", "cd!", and "." are valid, but "a!b" and "c.," are not valid).


Examples of valid words include "a-b.", "afad", "ba-c", "a!", and "!".

Given a string sentence, return the number of valid words in sentence.

&nbsp;
Example 1:

Input: sentence = "cat and  dog"
Output: 3
Explanation: The valid words in the sentence are "cat", "and", and "dog".


Example 2:

Input: sentence = "!this  1-s b8d!"
Output: 0
Explanation: There are no valid words in the sentence.
"!this" is invalid because it starts with a punctuation mark.
"1-s" and "b8d" are invalid because they contain digits.


Example 3:

Input: sentence = "alice and  bob are playing stone-game10"
Output: 5
Explanation: The valid words in the sentence are "alice", "and", "bob", "are", and "playing".
"stone-game10" is invalid because it contains digits.


&nbsp;
Constraints:


	1 &lt;= sentence.length &lt;= 1000
	sentence only contains lowercase English letters, digits, ' ', '-', '!', '.', and ','.
	There will be at least&nbsp;1 token.


import re
class Solution:
    def countValidWords(self, sentence: str) -> int:
        
        # parse and get each word from sentence
        words = sentence.split()
        
        # regular expression pattern for valid words
        pattern = re.compile( r'^([a-z]+\-?[a-z]+[!\.,]?)$|^([a-z]*[!\.,]?)$' )
        
        # valid word count
        count = 0
        
        # scan each word from word pool
        for word in words:
            
            # judge whether current word is valid or not
            match = re.match(pattern, word)
            
            if match:
                count+=1
                
        return count
class Solution {
    public int countValidWords(String sentence) {
        String regex = "^([a-z]+(-?[a-z]+)?)?(!|\\.|,)?$";
        String r2 = "[^0-9]+";
        String[] arr = sentence.split("\\s+");
        int ans = 0;
        for(String s: arr)
        {
            if(s.matches(regex) && s.matches(r2))
            {
                ans++;
                //System.out.println(s);
            }
        }
        return ans;
    }
}
#include <regex>

class Solution {
public:
    int countValidWords(string sentence) {

        int count = 0;

        // Defining the regex pattern
        regex valid_word("[a-z]*([a-z]-[a-z])?[a-z]*[!,.]?");

        // splitting the sentence to words
        stringstream s(sentence);
        string word;
        while(getline(s, word, ' ')) {

            // Checking if the word matches the regex pattern
            if(word != "" && regex_match(word, valid_word)){
                ++count;
            }
        }

        return count;
    }
};
/**
 * @param {string} sentence
 * @return {number}
 */
var countValidWords = function(sentence) {
    let list = sentence.split(' ')
    let filtered = list.filter(s => {
        if (/\d/.test(s) || s === '') return false   //removes anything with numbers or is blank
        if (/^[!,.]$/.test(s)) return true          //punctuation only
        if (/^\w+[!,.]?$/.test(s)) return true      //word + optional punctuation
        if (/^\w+[-]?\w+[!,.]?$/.test(s)) return true //word + optional hypen + word + optional punctuation
        return false
    })
    
    return filtered.length
};
Number of Valid Words in a Sentence