Valid Word
EasyUpdated: Jul 15, 2025
Practice on:
Problem
A word is considered valid if:
- It contains a minimum of 3 characters.
- It contains only digits (0-9), and English letters (uppercase and lowercase).
- It includes at least one vowel.
- It includes at least one consonant.
You are given a string word.
Return true if word is valid, otherwise, return false.
Notes:
'a','e','i','o','u', and their uppercases are vowels.- A consonant is an English letter that is not a vowel.
Examples
Example 1
Input: word = "234Adas"
Output: true
Explanation:
This word satisfies the conditions.
Example 2
Input: word = "b3"
Output: false
Explanation:
The length of this word is fewer than 3, and does not have a vowel.
Example 3
Input: word = "a3$e"
Output: false
Explanation:
This word contains a `'$'` character and does not have a consonant.
Constraints
1 <= word.length <= 20wordconsists of English uppercase and lowercase letters, digits,'@','#', and'$'.
Solution
Method 1 – Simple Checks
Intuition
We need to check the length, allowed characters, and presence of at least one vowel and one consonant.
Approach
- Check if the length is at least 3.
- Check if all characters are alphanumeric (letters or digits).
- Check for at least one vowel and at least one consonant.
Code
C++
bool isValid(string word) {
if (word.length() < 3) return false;
string vowels = "aeiouAEIOU";
bool hasVowel = false, hasConsonant = false;
for (char c : word) {
if (!isalnum(c)) return false;
if (isalpha(c)) {
if (vowels.find(c) != string::npos) hasVowel = true;
else hasConsonant = true;
}
}
return hasVowel && hasConsonant;
}
Go
import "unicode"
func isValid(word string) bool {
if len(word) < 3 {
return false
}
vowels := "aeiouAEIOU"
hasVowel, hasConsonant := false, false
for _, c := range word {
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
return false
}
if unicode.IsLetter(c) {
if strings.ContainsRune(vowels, c) {
hasVowel = true
} else {
hasConsonant = true
}
}
}
return hasVowel && hasConsonant
}
Java
class Solution {
public boolean isValid(String word) {
if (word.length() < 3) return false;
String vowels = "aeiouAEIOU";
boolean hasVowel = false, hasConsonant = false;
for (char c : word.toCharArray()) {
if (!Character.isLetterOrDigit(c)) return false;
if (Character.isLetter(c)) {
if (vowels.indexOf(c) >= 0) hasVowel = true;
else hasConsonant = true;
}
}
return hasVowel && hasConsonant;
}
}
Kotlin
fun isValid(word: String): Boolean {
if (word.length < 3) return false
val vowels = "aeiouAEIOU"
var hasVowel = false
var hasConsonant = false
for (c in word) {
if (!c.isLetterOrDigit()) return false
if (c.isLetter()) {
if (vowels.contains(c)) hasVowel = true
else hasConsonant = true
}
}
return hasVowel && hasConsonant
}
Python
def isValid(word: str) -> bool:
if len(word) < 3:
return False
vowels = set('aeiouAEIOU')
has_vowel = False
has_consonant = False
for c in word:
if not c.isalnum():
return False
if c.isalpha():
if c in vowels:
has_vowel = True
else:
has_consonant = True
return has_vowel and has_consonant
Rust
pub fn is_valid(word: &str) -> bool {
if word.len() < 3 { return false; }
let vowels = "aeiouAEIOU";
let (mut has_vowel, mut has_consonant) = (false, false);
for c in word.chars() {
if !c.is_ascii_alphanumeric() { return false; }
if c.is_ascii_alphabetic() {
if vowels.contains(c) { has_vowel = true; }
else { has_consonant = true; }
}
}
has_vowel && has_consonant
}
TypeScript
function isValid(word: string): boolean {
if (word.length < 3) return false;
const vowels = "aeiouAEIOU";
let hasVowel = false, hasConsonant = false;
for (const c of word) {
if (!/[a-zA-Z0-9]/.test(c)) return false;
if (/[a-zA-Z]/.test(c)) {
if (vowels.includes(c)) hasVowel = true;
else hasConsonant = true;
}
}
return hasVowel && hasConsonant;
}
Complexity
- ⏰ Time complexity:
O(n)— n is the length of the word. - 🧺 Space complexity:
O(1)— Only a few variables are used.