Post

3396 Valid Word

3396 Valid Word

Valid Word image

A word is considered valid if:

1
2
3
4
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:

1
2
'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
2
1 <= word.length <= 20
word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

impl Solution {
    pub fn is_valid(word: String) -> bool {
        if word.len() < 3 {
            return false;
        }
        let mut vowel = false;
        let mut consonant = false;
        for mut k in word.chars(){
            let m = k.to_ascii_lowercase() as u8;
            if !((m >= 48 && m <= 57 ) || (m >= 97 && m <= 122) ){
                return false;
            } 
            
            if k.to_ascii_lowercase() == 'a' || k.to_ascii_lowercase() == 'e' || k.to_ascii_lowercase() == 'i' || k.to_ascii_lowercase() == 'o' || k.to_ascii_lowercase() == 'u' {
                vowel = true;
            }else {
                if (m >= 97 && m <= 122) {
                    consonant = true;
                }
                
            }
        }
        return vowel && consonant;
    }
}



This post is licensed under CC BY 4.0 by the author.