Number of Valid Words in a Sentence - Problem

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.

Input & Output

Example 1 — Basic Validation
$ Input: sentence = "cat and dog"
Output: 3
💡 Note: All three words are valid: "cat" (only lowercase), "and" (only lowercase), "dog" (only lowercase). Multiple spaces between words don't affect token validation.
Example 2 — Invalid Words with Digits
$ Input: sentence = "!this 1-s b8d!"
Output: 0
💡 Note: "!this" is invalid (punctuation must be at end, not start), "1-s" is invalid (contains digit), "b8d!" is invalid (contains digit). No valid words.
Example 3 — Hyphen and Punctuation Rules
$ Input: sentence = "alice and bob are playing stone-game10"
Output: 5
💡 Note: "alice", "and", "bob", "are", "playing" are all valid. "stone-game10" is invalid due to digits. Hyphen in "stone-game10" would be valid if no digits were present.

Constraints

  • 1 ≤ sentence.length ≤ 1000
  • sentence only contains lowercase English letters, digits, spaces, hyphens, and punctuation marks

Visualization

Tap to expand
Number of Valid Words in a Sentence INPUT Sentence String: "cat and dog" Tokens (split by spaces): "cat" "and" "dog" Validation Rules: 1. No digits allowed 2. Max 1 hyphen (surrounded) 3. Max 1 punctuation (at end) Valid: "a-b.", "a!", "." Invalid: "-ab", "a!b", "1a" ALGORITHM STEPS 1 Split Tokens Separate by spaces 2 Scan Each Char Check digit, hyphen, punct 3 Validate Hyphen Must be surrounded 4 Count Valid Increment counter Processing Tokens: "cat" --> OK (letters only) "and" --> OK (letters only) "dog" --> OK (letters only) Count = 3 FINAL RESULT Valid Words Found: "cat" "and" "dog" Output Value: 3 All 3 tokens are valid: Only lowercase letters, no digits, hyphens, or punct Time: O(n) | Space: O(1) n = sentence length Key Insight: Single pass scanning: For each token, track hyphen count, punctuation count, and their positions. A token is invalid immediately if: (1) it contains a digit, (2) hyphen not surrounded by letters, (3) more than one hyphen or punctuation, or (4) punctuation appears before the end. TutorialsPoint - Number of Valid Words in a Sentence | Single Pass Character Scanning
Asked in
Google 15 Microsoft 12 Amazon 8
25.0K Views
Medium Frequency
~15 min Avg. Time
890 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