Problem · String

Number of Valid Words

Learn this problem
EasyIBM logoIBMFULLTIMEOA
See IBM hiring insights

Problem statement

Given a string s, split it into whitespace-delimited words and return the number of valid words.

A word is valid when all of the following are true:

  • It contains at least 3 characters.
  • Every character is alphanumeric: 0-9, A-Z, or a-z.
  • It contains at least one vowel: a, e, i, o, or u, in either letter case.
  • It contains at least one consonant.

Digits are allowed, but they are neither vowels nor consonants.

Function

numberOfValidWords(s: String) → int

Examples

Example 1

s = "This is an example string 234."return = 3

The valid words are This, example, and string. The words is and an are too short, while 234. contains a non-alphanumeric character.

Example 2

s = "Bos wins the game"return = 4

All four words contain at least three alphanumeric characters, at least one vowel, and at least one consonant. Therefore the result is 4.

Constraints

  • 1 ≤ s.length ≤ 10^5
  • s contains printable ASCII characters.
  • Words are separated by one or more whitespace characters.

More IBM problems

drafts saved locally
public int numberOfValidWords(String s) {
    // Write your code here
}
s"This is an example string 234."
expected3
checking account