Problem Description
Given a sentence containing tokens separated by spaces, count the number of tokens (words) that are valid. A valid word may include lowercase letters, at most one hyphen (which must be surrounded by letters), and at most one punctuation mark (if it exists, it must be at the end of the token). Digits are not allowed.
Key Insights
- Split the sentence into tokens by spaces.
- Each token must be checked for the following:
- It contains only valid characters (lowercase letters, hyphens, and punctuation marks) and no digits.
- It has at most one hyphen, and if present the hyphen must be flanked by lowercase letters.
- It has at most one punctuation mark, and if present it must be located at the end of the token.
- Use iteration to verify each token and count the valid ones.
Space and Time Complexity
Time Complexity: O(n * m), where n is the number of tokens and m is the average length of a token. Space Complexity: O(n) for storing the tokenized words, though auxiliary space remains constant.
Solution
We solve this problem by:
- Splitting the input sentence into tokens using spaces as delimiters.
- For each token, performing the following checks:
- Verify that the token does not contain any digits.
- Count the number of hyphens. If a hyphen is found, ensure it is neither at the beginning nor at the end, and that the characters immediately before and after are lowercase letters.
- Count the number of punctuation marks (from the set {!,.,,}). If a punctuation mark exists, ensure it appears only at the very end of the token.
- Count tokens that satisfy all criteria. The solution primarily uses string manipulation and iteration through characters.