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#
1
2
3
4
5
6
7
8
Input: word = "234Adas"
Output: true
Explanation:
This word satisfies the conditions.
Example 2#
1
2
3
4
5
6
7
8
Input: word = "b3"
Output: false
Explanation:
The length of this word is fewer than 3 , and does not have a vowel.
Example 3#
1
2
3
4
5
6
7
8
Input: word = "a3$e"
Output: false
Explanation:
This word contains a `'$'` character and does not have a consonant.
Constraints#
1 <= word.length <= 20
word
consists 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#
Cpp
Go
Java
Kotlin
Python
Rust
Typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
}
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
1
2
3
4
5
6
7
8
9
10
11
12
13
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
}
1
2
3
4
5
6
7
8
9
10
11
12
13
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.