Valid Word
Problem
A word is valid when it satisfies all of these rules: it has a minimum of 3 characters; it contains only digits (0–9) and English letters (upper or lower case); it includes at least one vowel (a, e, i, o, u and their uppercases); and it includes at least one consonant (any English letter that is not a vowel). Given a string word, return true if it is valid, otherwise false.
word = "234Adas"truedef isValid(word):
if len(word) < 3: # rule 1: at least 3 characters
return False
vowels = set("aeiouAEIOU")
has_vowel = False
has_consonant = False
for ch in word: # scan every character
if ch.isalpha(): # an English letter
if ch in vowels:
has_vowel = True
else:
has_consonant = True
elif not ch.isdigit(): # not a letter, not a digit
return False # illegal character (e.g. '$')
return has_vowel and has_consonant # need a vowel AND a consonant
function isValid(word) {
if (word.length < 3) return false; // rule 1: at least 3 characters
const vowels = new Set([..."aeiouAEIOU"]);
let hasVowel = false, hasConsonant = false;
for (const ch of word) { // scan every character
if (/[a-zA-Z]/.test(ch)) { // an English letter
if (vowels.has(ch)) hasVowel = true;
else hasConsonant = true;
} else if (!/[0-9]/.test(ch)) { // not a letter, not a digit
return false; // illegal character (e.g. '$')
}
}
return hasVowel && hasConsonant; // need a vowel AND a consonant
}
boolean isValid(String word) {
if (word.length() < 3) return false; // rule 1: at least 3 characters
String vowels = "aeiouAEIOU";
boolean hasVowel = false, hasConsonant = false;
for (char ch : word.toCharArray()) { // scan every character
if (Character.isLetter(ch)) { // an English letter
if (vowels.indexOf(ch) >= 0) hasVowel = true;
else hasConsonant = true;
} else if (!Character.isDigit(ch)) { // not a letter, not a digit
return false; // illegal character (e.g. '$')
}
}
return hasVowel && hasConsonant; // need a vowel AND a consonant
}
bool isValid(string word) {
if (word.size() < 3) return false; // rule 1: at least 3 characters
string vowels = "aeiouAEIOU";
bool hasVowel = false, hasConsonant = false;
for (char ch : word) { // scan every character
if (isalpha((unsigned char)ch)) { // an English letter
if (vowels.find(ch) != string::npos) hasVowel = true;
else hasConsonant = true;
} else if (!isdigit((unsigned char)ch)) { // not a letter, not a digit
return false; // illegal character (e.g. '$')
}
}
return hasVowel && hasConsonant; // need a vowel AND a consonant
}
Explanation
This is a pure validation problem: there is no clever data structure, just a careful check of four independent rules over a short string (length up to 20).
We start with the cheapest test — the length. If the word has fewer than 3 characters it can never be valid, so we return false immediately and avoid scanning anything.
Then we make a single pass over the characters, carrying two boolean flags: has_vowel and has_consonant. For each character we classify it. If it is an English letter, we ask whether it is one of aeiouAEIOU: a match sets the vowel flag, anything else sets the consonant flag. If it is a digit we simply ignore it — digits are allowed but contribute to neither flag.
If a character is neither a letter nor a digit (for example '$', '@', or '#'), the word breaks the “only digits and letters” rule, so we can stop early and return false.
After the scan, the word is valid exactly when both flags are true: at least one vowel and at least one consonant were seen. We return has_vowel and has_consonant.
Example: "a3$e" is length 4 and has vowels 'a', 'e', but the '$' is an illegal character — and there is no consonant either — so the answer is false.