Practice Number of Valid Words in a Sentence
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Split sentence into nonempty tokens at spaces. A valid token contains only lowercase letters, an optional hyphen, and an optional punctuation mark from !, ., or ,. It contains no digits. There may be at most one hyphen, and it must have a lowercase letter immediately on both sides. There may be at most one punctuation mark, and it must be the final character. A punctuation-only token such as ! is valid. Return the number of valid tokens.
Source: Number of Valid Words in a Sentence on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
sentence = "cat and dog"
3
# Example 2
sentence = "!this 1-s b8d!"
0
# Example 3
sentence = "alice and bob are playing stone-game10"
5
Constraints
Related LeetCode Problems
Practice more problems similar to Number of Valid Words in a Sentence.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.