3396. Valid Word
My accepted C++ solution to LeetCode problem 3396, Valid Word, running in 0ms.
- Difficulty: Easy
- C++
- Runtime 0ms
- Memory 8.1MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 8.1MB, accepted 2025-12-27.
class Solution {
public:
bool isValid(string word) {
if (word.size() < 3) return false;
bool hasVowel = false, hasCons = false;
for (char c : word) {
if (!isalnum(c)) return false;
if (isalpha(c)) {
char lower = tolower(c);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u')
hasVowel = true;
else
hasCons = true;
}
}
return hasVowel && hasCons;
}
};