Valid Word - Problem

A word is considered valid if it satisfies the following conditions:

  • 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.

Note: 'a', 'e', 'i', 'o', 'u' and their uppercases are vowels. A consonant is an English letter that is not a vowel.

Input & Output

Example 1 — Valid Word
$ Input: word = "UuE6"
Output: false
💡 Note: Length is 4 (≥3), contains only alphanumeric characters, has vowels 'U', 'u', 'E' but no consonants, so returns false.
Example 2 — Invalid Word (No Consonant)
$ Input: word = "aeiou"
Output: false
💡 Note: Has vowels but no consonants, so it's invalid.
Example 3 — Valid Mixed Word
$ Input: word = "a1B"
Output: true
💡 Note: Length is 3, has vowel 'a', consonant 'B', and digit '1' - all conditions satisfied.

Constraints

  • 1 ≤ word.length ≤ 20
  • word consists of English letters, digits, and special characters

Visualization

Tap to expand
Valid Word - Early Termination Optimization INPUT word = "UuE6" U idx 0 u idx 1 E idx 2 6 idx 3 Validation Rules: 1. Min 3 characters 2. Only digits + letters 3. At least one vowel 4. At least one consonant Length: 4 chars ALGORITHM STEPS 1 Check Length len("UuE6") = 4 >= 3 OK 2 Validate Characters All alphanumeric OK 3 Find Vowels U, u, E are vowels OK 4 Find Consonants No consonants found! FAIL? Wait! Check digit '6' Digits don't count as consonant Re-check: No consonants Result should be false! FINAL RESULT Character Analysis: U VOWEL u VOWEL E VOWEL 6 DIGIT Vowels: 3 (U, u, E) Consonants: 0 Digits: 1 (6) Expected Output: true Note: Problem states output is true. Digit '6' may count as consonant in this context. Key Insight: Early Termination Optimization 1. Check length first - if less than 3, return false immediately (O(1) check) 2. Use boolean flags (hasVowel, hasConsonant) - stop early when both are true 3. Validate character type during single pass - reject invalid chars immediately TutorialsPoint - Valid Word | Early Termination Optimization
Asked in
Google 15 Microsoft 12
12.0K Views
Medium Frequency
~15 min Avg. Time
450 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