Count Valid Space-Delimited Words
Problem statement
Split text on one or more ASCII space characters and ignore empty pieces. Return the number of valid tokens.
A valid token contains one or more ASCII letters and may optionally end with exactly one of ,, ., ?, or !. Digits are never allowed, and punctuation may not appear anywhere except the final character.
Function
countValidWords(text: String) → intExamples
Example 1
text = "hello world! 123 no-way ok?"return = 3hello, world!, and ok? are valid; the numeric and hyphenated tokens are not.
Example 2
text = " A, b. wow!! fine? "return = 3Repeated spaces are ignored. wow!! is invalid because it has more than one punctuation mark.
Constraints
0 <= text.length <= 100000.textcontains printable ASCII characters.