#2047
Number of Valid Words in a Sentence
pupil · 575 · lc easy +29 · verified · 31% accepted · 352 likes · top 8%
Description
A sentence is composed of space-separated tokens (possibly multiple spaces). A token is a valid word when all three conditions hold: (1) it contains no digits; (2) it has at most one hyphen '-', which must be between two lowercase letters; (3) it has at most one punctuation mark ('!', '.', or ','), which must be at the very end.
Return the number of valid words in sentence.
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.
Code
1
2
3