2025-07-15 Daily Challenge
Today I have done leetcode's July LeetCoding Challenge with cpp
.
July LeetCoding Challenge 15
Description
Valid Word
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'$'
.
Solution
class Solution {
bool isvowel(char c) {
switch(c) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
default:
}
return false;
}
public:
bool isValid(string word) {
if(word.size() < 3) return false;
bool hasVowel = false;
bool hasConsonant = false;
for(auto c : word) {
if(!isalnum(c)) return false;
hasVowel = isvowel(c) || hasVowel;
hasConsonant = (isalpha(c) && !isvowel(c)) || hasConsonant;
}
return hasConsonant && hasVowel;
}
};
// Accepted
// 677/677 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 33.95 % of cpp submissions (8 MB)