Number of Valid Words in a Sentence - Problem

Imagine you're building a smart text processor that needs to validate words in sentences! ๐Ÿ”

You're given a sentence containing lowercase letters, digits, hyphens, punctuation marks (!, ., ,), and spaces. Your task is to count how many valid words exist in this sentence.

A word is considered valid if it follows these strict rules:

  • Character Rule: Only lowercase letters, hyphens, and punctuation allowed (no digits!)
  • Hyphen Rule: At most one hyphen, and it must be surrounded by lowercase letters ("a-b" โœ…, "-ab" โŒ, "ab-" โŒ)
  • Punctuation Rule: At most one punctuation mark, and it must be at the very end ("hello!" โœ…, "he!llo" โŒ)

Examples of valid words: "a-b.", "hello", "world!", "x-y", "!"

Goal: Return the total count of valid words in the sentence.

Input & Output

example_1.py โ€” Basic Mixed Sentence
$ Input: sentence = "cat and dog"
โ€บ Output: 3
๐Ÿ’ก Note: All three tokens 'cat', 'and', and 'dog' are valid words (only lowercase letters, no digits, no hyphens, no punctuation)
example_2.py โ€” Complex Validation
$ Input: sentence = "!this 1-s b8d!"
โ€บ Output: 2
๐Ÿ’ก Note: '!this' is valid (punctuation only at end), '1-s' is invalid (contains digit), 'b8d!' is invalid (contains digit)
example_3.py โ€” Hyphen Edge Cases
$ Input: sentence = "alice and bob are playing stone-game10"
โ€บ Output: 5
๐Ÿ’ก Note: 'alice', 'and', 'bob', 'are', 'playing' are valid. 'stone-game10' is invalid due to digit '1' and '0'

Constraints

  • 1 โ‰ค sentence.length โ‰ค 1000
  • sentence consists of lowercase letters, digits, hyphens, punctuation marks, and spaces only
  • There are no leading or trailing spaces in sentence
  • Each token is separated by at least one space

Visualization

Tap to expand
Word Validation Factory PipelineInput Sentence"cat and-dog5!"Token SplitterSplit by spacesValidation StationMulti-rule inspectorcatand-dog5!Validation Rules Applied:โœ“ 'cat': No digits, no hyphens, no punctuation - VALIDโœ— 'and-dog5!': Contains digit '5' - INVALIDFinal result: 1 valid wordQuality Control Results:โ€ข Accepted: 1 word passed all validation rulesโ€ข Rejected: 1 word failed digit validation
Understanding the Visualization
1
Token Splitting
Sentence 'cat and-dog!' gets split into ['cat', 'and-dog!']
2
Rule Inspection
Each token flows through validation: digits check, hyphen rules, punctuation rules
3
Quality Control
Invalid tokens (with digits, bad hyphens, misplaced punctuation) get rejected
4
Valid Count
Only tokens passing all quality checks contribute to the final count
Key Takeaway
๐ŸŽฏ Key Insight: Single-pass validation is like having an expert inspector who can check multiple quality standards simultaneously, making the process both faster and more efficient than multiple separate inspections.
Asked in
Meta 12 Google 8 Amazon 6 Microsoft 4
28.4K Views
Medium Frequency
~15 min Avg. Time
854 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen