Valid Word

easy string validation vowels

Problem

A word is valid when it satisfies all of these rules: it has a minimum of 3 characters; it contains only digits (0–9) and English letters (upper or lower case); it includes at least one vowel (a, e, i, o, u and their uppercases); and it includes at least one consonant (any English letter that is not a vowel). Given a string word, return true if it is valid, otherwise false.

Inputword = "234Adas"
Outputtrue
Length 7 ≥ 3, every character is a digit or letter, ‘A’ and ‘a’ are vowels, and ‘d’, ‘s’ are consonants — all conditions hold.

def isValid(word):
    if len(word) < 3:                       # rule 1: at least 3 characters
        return False
    vowels = set("aeiouAEIOU")
    has_vowel = False
    has_consonant = False
    for ch in word:                         # scan every character
        if ch.isalpha():                    # an English letter
            if ch in vowels:
                has_vowel = True
            else:
                has_consonant = True
        elif not ch.isdigit():              # not a letter, not a digit
            return False                    # illegal character (e.g. '$')
    return has_vowel and has_consonant      # need a vowel AND a consonant
function isValid(word) {
  if (word.length < 3) return false;        // rule 1: at least 3 characters
  const vowels = new Set([..."aeiouAEIOU"]);
  let hasVowel = false, hasConsonant = false;
  for (const ch of word) {                   // scan every character
    if (/[a-zA-Z]/.test(ch)) {               // an English letter
      if (vowels.has(ch)) hasVowel = true;
      else hasConsonant = true;
    } else if (!/[0-9]/.test(ch)) {          // not a letter, not a digit
      return false;                          // illegal character (e.g. '$')
    }
  }
  return hasVowel && hasConsonant;          // need a vowel AND a consonant
}
boolean isValid(String word) {
    if (word.length() < 3) return false;     // rule 1: at least 3 characters
    String vowels = "aeiouAEIOU";
    boolean hasVowel = false, hasConsonant = false;
    for (char ch : word.toCharArray()) {     // scan every character
        if (Character.isLetter(ch)) {        // an English letter
            if (vowels.indexOf(ch) >= 0) hasVowel = true;
            else hasConsonant = true;
        } else if (!Character.isDigit(ch)) { // not a letter, not a digit
            return false;                    // illegal character (e.g. '$')
        }
    }
    return hasVowel && hasConsonant;         // need a vowel AND a consonant
}
bool isValid(string word) {
    if (word.size() < 3) return false;       // rule 1: at least 3 characters
    string vowels = "aeiouAEIOU";
    bool hasVowel = false, hasConsonant = false;
    for (char ch : word) {                   // scan every character
        if (isalpha((unsigned char)ch)) {    // an English letter
            if (vowels.find(ch) != string::npos) hasVowel = true;
            else hasConsonant = true;
        } else if (!isdigit((unsigned char)ch)) { // not a letter, not a digit
            return false;                    // illegal character (e.g. '$')
        }
    }
    return hasVowel && hasConsonant;         // need a vowel AND a consonant
}
Time: O(n) Space: O(1)