Valid Word — Solution in Python, Java & C++
Valid Word solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem, asked at Amazon and Google.
Valid Word is an easy coding problem on strings, asked in Amazon and Google interviews.
- Difficulty: Easy
- Topics: Strings
- Asked at: Amazon, Google
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A word is valid when all of the following hold:
- it has at least
3characters; - it contains only digits
0–9and English letters (uppercase or lowercase); - it contains at least one vowel (
a,e,i,o,uin either case); - it contains at least one consonant (any English letter that is not a vowel).
Given a string word, return true if it is valid, otherwise false.
Example 1
Input: word = "234Adas"
Output: true
Explanation: Seven characters, only digits and letters, with vowel 'A' and consonant 'd'.
Example 2
Input: word = "b3"
Output: false
Explanation: Fewer than three characters, and no vowel.
Example 3
Input: word = "a3$e"
Output: false
Explanation: '$' is neither a digit nor a letter.
Constraints
1 <= word.length <= 20word consists of English uppercase and lowercase letters, digits, '@', '#' and '$'
Hints for Valid Word
Hint 1
Check the length first; then look at every character once.
Hint 2
A character that is neither a letter nor a digit makes the word invalid immediately.
Hint 3
Track two flags — saw a vowel, saw a consonant — and lowercase a letter before testing whether it is a vowel. Digits set neither flag.
How to solve Valid Word
Each rule is a property of single characters or of the length, so one pass with two flags checks them all.
Approach
- If
word.length < 3, returnfalse. - For each character: if it is a letter, set
vowelwhen its lowercase form is inaeiou, otherwise setconsonant; if it is a digit, do nothing; otherwise returnfalse. - Return
vowel && consonant.
Why it works
The pass rejects any forbidden character, and after it the two flags say exactly whether a vowel and a consonant occurred, which with the length check covers all four rules.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Uppercase vowels count as vowels: "AbC" is valid.
- Digits are allowed but are not consonants — "a12" has no consonant.
- Testing for 'not a vowel' without first checking it is a letter would count digits or symbols as consonants.
Reference solution
Python
def isValid(word: str) -> bool:
if len(word) < 3:
return False
vowel = consonant = False
for c in word:
if "a" <= c <= "z" or "A" <= c <= "Z":
if c in "aeiouAEIOU":
vowel = True
else:
consonant = True
elif not ("0" <= c <= "9"):
return False
return vowel and consonantJava
public static boolean isValid(String word) {
if (word.length() < 3) return false;
boolean vowel = false, consonant = false;
for (char c : word.toCharArray()) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
if ("aeiouAEIOU".indexOf(c) >= 0) vowel = true;
else consonant = true;
} else if (!(c >= '0' && c <= '9')) {
return false;
}
}
return vowel && consonant;
}C++
bool isValid(string word) {
if (word.size() < 3) return false;
bool vowel = false, consonant = false;
for (char c : word) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
char l = (char) tolower(c);
if (l == 'a' || l == 'e' || l == 'i' || l == 'o' || l == 'u') vowel = true;
else consonant = true;
} else if (!(c >= '0' && c <= '9')) {
return false;
}
}
return vowel && consonant;
}JavaScript
var isValid = function(word) {
if (word.length < 3) return false;
var vowel = false, consonant = false;
for (var i = 0; i < word.length; i++) {
var c = word[i];
if ((c >= "a" && c <= "z") || (c >= "A" && c <= "Z")) {
if ("aeiouAEIOU".indexOf(c) >= 0) vowel = true;
else consonant = true;
} else if (!(c >= "0" && c <= "9")) {
return false;
}
}
return vowel && consonant;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings