Welcome to Subscribe On Youtube

3136. Valid Word

Description

A word is considered valid if:

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

Notes:

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

 

Example 1:

Input: word = "234Adas"

Output: true

Explanation:

This word satisfies the conditions.

Example 2:

Input: word = "b3"

Output: false

Explanation:

The length of this word is fewer than 3, and does not have a vowel.

Example 3:

Input: word = "a3$e"

Output: false

Explanation:

This word contains a '$' character and does not have a consonant.

 

Constraints:

  • 1 <= word.length <= 20
  • word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.

Solutions

Solution 1: Simulation

First, we check if the length of the string is less than 3. If it is, we return false.

Next, we iterate through the string, checking if each character is a letter or a number. If it’s not, we return false. Otherwise, we check if the character is a vowel. If it is, we set has_vowel to true. If it’s not, we set has_consonant to true.

Finally, if both has_vowel and has_consonant are true, we return true. Otherwise, we return false.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the string.

  • class Solution {
        public boolean isValid(String word) {
            if (word.length() < 3) {
                return false;
            }
            boolean hasVowel = false, hasConsonant = false;
            boolean[] vs = new boolean[26];
            for (char c : "aeiou".toCharArray()) {
                vs[c - 'a'] = true;
            }
            for (char c : word.toCharArray()) {
                if (Character.isAlphabetic(c)) {
                    if (vs[Character.toLowerCase(c) - 'a']) {
                        hasVowel = true;
                    } else {
                        hasConsonant = true;
                    }
                } else if (!Character.isDigit(c)) {
                    return false;
                }
            }
            return hasVowel && hasConsonant;
        }
    }
    
  • class Solution {
    public:
        bool isValid(string word) {
            if (word.size() < 3) {
                return false;
            }
            bool has_vowel = false, has_consonant = false;
            bool vs[26]{};
            string vowels = "aeiou";
            for (char c : vowels) {
                vs[c - 'a'] = true;
            }
            for (char c : word) {
                if (isalpha(c)) {
                    if (vs[tolower(c) - 'a']) {
                        has_vowel = true;
                    } else {
                        has_consonant = true;
                    }
                } else if (!isdigit(c)) {
                    return false;
                }
            }
            return has_vowel && has_consonant;
        }
    };
    
  • class Solution:
        def isValid(self, word: str) -> bool:
            if len(word) < 3:
                return False
            has_vowel = has_consonant = False
            vs = set("aeiouAEIOU")
            for c in word:
                if not c.isalnum():
                    return False
                if c.isalpha():
                    if c in vs:
                        has_vowel = True
                    else:
                        has_consonant = True
            return has_vowel and has_consonant
    
    
  • func isValid(word string) bool {
    	if len(word) < 3 {
    		return false
    	}
    	hasVowel := false
    	hasConsonant := false
    	vs := make([]bool, 26)
    	for _, c := range "aeiou" {
    		vs[c-'a'] = true
    	}
    	for _, c := range word {
    		if unicode.IsLetter(c) {
    			if vs[unicode.ToLower(c)-'a'] {
    				hasVowel = true
    			} else {
    				hasConsonant = true
    			}
    		} else if !unicode.IsDigit(c) {
    			return false
    		}
    	}
    	return hasVowel && hasConsonant
    }
    
  • function isValid(word: string): boolean {
        if (word.length < 3) {
            return false;
        }
        let hasVowel: boolean = false;
        let hasConsonant: boolean = false;
        const vowels: Set<string> = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
        for (const c of word) {
            if (!c.match(/[a-zA-Z0-9]/)) {
                return false;
            }
            if (/[a-zA-Z]/.test(c)) {
                if (vowels.has(c)) {
                    hasVowel = true;
                } else {
                    hasConsonant = true;
                }
            }
        }
        return hasVowel && hasConsonant;
    }
    
    

All Problems

All Solutions