LeetCode solutions

3396. Valid Word

My accepted C++ solution to LeetCode problem 3396, Valid Word, running in 0ms.

  • Difficulty: Easy
  • C++
  • Runtime 0ms
  • Memory 8.1MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 8.1MB, accepted 2025-12-27.

cpp
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;
    }
};

Source