Valid Word (LeetCode 3136): Solution and Explanation
A word is valid when it has at least 3 characters, uses only letters and digits, and contains at least one vowel and at least one consonant. Check the length first, then read the word once, rejecting any other character on sight and setting a vowel flag and a consonant flag. "234Adas" is valid; "a3$e" is not.
Intuition
There are four independent rules, and each one can be decided while reading the word once.
Sort every character into one of four bins: vowel, consonant, digit, or not allowed. A single character in the last bin decides the answer immediately.
Digits are allowed but help neither the vowel rule nor the consonant rule, which is the easy detail to miss.
Approach: One pass with two flags
- If the word has fewer than 3 characters, return false.
- Set
hasVowelandhasConsonantto false. - For each character: if it is not a letter or digit, return false. If it is one of
aeiouAEIOU, sethasVowel. If it is any other letter, sethasConsonant. - Return
hasVowel && hasConsonant.
Step-by-step example
word = "234Adas"
- Length 7 is at least 3, so the length rule passes.
2,3,4are digits: allowed, but no flag changes.Ais a vowel, sohasVowel = true.dis a consonant, sohasConsonant = true.aandschange nothing new.- Both flags are true and no illegal character appeared, so the answer is true.
Code: Python, Java, C++
1class Solution:
2 def isValid(self, word: str) -> bool:
3 if len(word) < 3:
4 return False
5 has_vowel = has_consonant = False
6 for c in word:
7 if not c.isalnum():
8 return False
9 if c.isalpha():
10 if c.lower() in "aeiou":
11 has_vowel = True
12 else:
13 has_consonant = True
14 return has_vowel and has_consonant1class Solution {
2 public boolean isValid(String word) {
3 if (word.length() < 3) {
4 return false;
5 }
6 boolean hasVowel = false, hasConsonant = false;
7 for (char c : word.toCharArray()) {
8 if (!Character.isLetterOrDigit(c)) {
9 return false;
10 }
11 if (Character.isLetter(c)) {
12 if ("aeiouAEIOU".indexOf(c) >= 0) {
13 hasVowel = true;
14 } else {
15 hasConsonant = true;
16 }
17 }
18 }
19 return hasVowel && hasConsonant;
20 }
21}1class Solution {
2public:
3 bool isValid(string word) {
4 if (word.size() < 3) {
5 return false;
6 }
7 bool hasVowel = false, hasConsonant = false;
8 for (char c : word) {
9 if (!isalnum(c)) {
10 return false;
11 }
12 if (isalpha(c)) {
13 if (string("aeiouAEIOU").find(c) != string::npos) {
14 hasVowel = true;
15 } else {
16 hasConsonant = true;
17 }
18 }
19 }
20 return hasVowel && hasConsonant;
21 }
22};Time and space complexity
O(n)
Each character is classified once.
O(1)
Only two boolean flags are kept.
Other approaches
Regular expressions (time O(n), space O(1))
Test the whole word against ^[a-zA-Z0-9]{3,}$, then search for [aeiouAEIOU] and [b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]. Compact, but three scans and harder to read.
Count by category (time O(n), space O(1))
Count vowels, consonants, digits and other characters, then check that others is 0, vowels and consonants are positive, and the total is at least 3. Same pass, without the early exit.
Common mistakes
- Treating digits as consonants.
"a12"has no consonant and is not valid. - Checking vowels in lowercase only.
"AB1"is valid becauseAis a vowel. - Forgetting the length rule, which makes
"ab"look valid.
Frequently asked questions
Is 'y' a vowel here?
No. Only a, e, i, o and u in either case are vowels, so 'y' is a consonant.
Why return as soon as an illegal character appears?
One illegal character makes the word invalid no matter what follows, so reading further cannot change the answer.
Can a word with only digits be valid?
No. It has no letters at all, so both the vowel and the consonant rules fail.