Problem · String
Number of Valid Words
Learn this problemProblem 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
3characters. - Every character is alphanumeric:
0-9,A-Z, ora-z. - It contains at least one vowel:
a,e,i,o, oru, in either letter case. - It contains at least one consonant.
Digits are allowed, but they are neither vowels nor consonants.
Function
numberOfValidWords(s: String) → intExamples
Example 1
s = "This is an example string 234."return = 3The 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 = 4All 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^5scontains printable ASCII characters.- Words are separated by one or more whitespace characters.
More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026