Valid Word - Problem
A valid word is like a well-formed password that follows specific rules to ensure it contains meaningful content. Your task is to determine if a given string qualifies as a valid word based on four essential criteria:
- Length Requirement: Must contain at least 3 characters
- Character Set: Only digits (0-9) and English letters (a-z, A-Z) are allowed
- Vowel Requirement: Must include at least one vowel ('a', 'e', 'i', 'o', 'u' and their uppercase versions)
- Consonant Requirement: Must include at least one consonant (any English letter that is not a vowel)
Given a string word, return true if the word is valid according to all four rules, otherwise return false.
Example: "abc3" is valid (length โฅ 3, contains vowel 'a', consonant 'b', 'c', and digit '3'), but "a1" is invalid (too short and no consonant).
Input & Output
example_1.py โ Valid word with mixed characters
$
Input:
word = "234Adas"
โบ
Output:
true
๐ก Note:
The word '234Adas' is valid because: length is 7 (โฅ3), contains only alphanumeric characters, has vowels 'A' and 'a', and has consonants 'd', 's'.
example_2.py โ Invalid word missing vowel
$
Input:
word = "b3"
โบ
Output:
false
๐ก Note:
The word 'b3' is invalid because: length is 2 (<3), and it has no vowels (only consonant 'b' and digit '3').
example_3.py โ Invalid word with special characters
$
Input:
word = "a3$e"
โบ
Output:
false
๐ก Note:
The word 'a3$e' is invalid because it contains the special character '$' which is not alphanumeric, even though it has the required length, vowels, and consonants.
Visualization
Tap to expand
Understanding the Visualization
1
Character Enters
Each character is examined for validity (alphanumeric check)
2
Type Classification
Valid letters are classified as vowels or consonants, digits are noted
3
Flag Updates
Boolean flags are updated based on character classification
4
Final Decision
After processing all characters, check length and both flags
Key Takeaway
๐ฏ Key Insight: Single-pass validation with boolean flags is the most efficient approach, checking all criteria simultaneously while iterating through the string only once.
Time & Space Complexity
Time Complexity
O(n)
Single pass through the string of length n
โ Linear Growth
Space Complexity
O(1)
Only using boolean flags and character checks
โ Linear Space
Constraints
- 1 โค word.length โค 20
- word consists of English letters (uppercase and lowercase), digits, and/or symbols.
- Vowels are: 'a', 'e', 'i', 'o', 'u' and their uppercase versions
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code