FastPrepCount Valid Space-Delimited Words

Count Valid Space-Delimited Words

Visa logoVisa● EasyFULLTIMEOA
Learn

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) → int

Examples

Example 1

text = "hello world! 123 no-way ok?"return = 3

hello, world!, and ok? are valid; the numeric and hyphenated tokens are not.

Example 2

text = "  A, b. wow!! fine?  "return = 3

Repeated spaces are ignored. wow!! is invalid because it has more than one punctuation mark.

Constraints

  • 0 <= text.length <= 100000.
  • text contains printable ASCII characters.

More Visa problems

See Visa hiring insights
public int countValidWords(String text) {
    // Write your code here.
}
text"hello world! 123 no-way ok?"
expected3
Checking account…