[LeetCode] Valid Word

3136. Valid Word

A word is considered valid if:

  • It contains a minimum of 3 characters.
  • It consists of the digits 0-9, and the uppercase and lowercase English letters. (Not necessary to have all of them.)
  • 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
bool isValid(string word) {
unordered_set<char> v{'a','e','i','o','u','A','E','I','O','U'};
bool vo = 0, vvo = 0;
if(word.size() < 3) return false;
for(auto& ch : word) {
if(isdigit(ch)) continue;
if(!isalpha(ch)) return false;
if(v.count(ch)) vo = true;
else vvo = true;
}
return vo and vvo;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/05/PS/LeetCode/valid-word/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.