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
Word Validation Assembly LineINPUTLength Checkโ‰ฅ 3 charsCharacterValidationVowel FlagConsonant FlagFinalDecisionhelpa5consonantvowelconsonantconsonantvoweldigitโœ“ VALIDATION PASSEDLength: 6 โ‰ฅ 3Has vowels: e, aHas consonants: h, l, p
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using boolean flags and character checks

n
2n
โœ“ 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
Asked in
Meta 25 Amazon 20 Google 15 Microsoft 12
24.6K Views
Medium Frequency
~12 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen