bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2047. Number of Valid Words in a Sentence

2047. Number of Valid Words in a Sentence

Leetcode Solutions

Iterative Token Validation

  1. Split the sentence into tokens using space as the delimiter.
  2. Initialize a counter to keep track of valid words.
  3. Iterate over each token in the list of tokens.
  4. For each token, check if it contains any digits; if so, skip to the next token.
  5. Check if the token has more than one hyphen or punctuation mark; if so, skip to the next token.
  6. If there is a hyphen, check that it is surrounded by lowercase letters; if not, skip to the next token.
  7. If there is a punctuation mark, check that it is at the end of the token; if not, skip to the next token.
  8. If the token passes all checks, increment the valid word counter.
  9. After iterating through all tokens, return the valid word counter as the result.
UML Thumbnail

Regex-based Token Validation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...