0
Leetcode Problem 2047. Number of Valid Words in a Sentence
2047. Number of Valid Words in a Sentence
AI Mock Interview
Leetcode Solutions
Iterative Token Validation
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Split the sentence into tokens using space as the delimiter.
Initialize a counter to keep track of valid words.
Iterate over each token in the list of tokens.
For each token, check if it contains any digits; if so, skip to the next token.
Check if the token has more than one hyphen or punctuation mark; if so, skip to the next token.
If there is a hyphen, check that it is surrounded by lowercase letters; if not, skip to the next token.
If there is a punctuation mark, check that it is at the end of the token; if not, skip to the next token.
If the token passes all checks, increment the valid word counter.
After iterating through all tokens, return the valid word counter as the result.
Regex-based Token Validation
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...