Valid Word (LeetCode 3136): Solution and Explanation

Easy
String

A word is valid when it has at least 3 characters, uses only letters and digits, and contains at least one vowel and at least one consonant. Check the length first, then read the word once, rejecting any other character on sight and setting a vowel flag and a consonant flag. "234Adas" is valid; "a3$e" is not.

Intuition

There are four independent rules, and each one can be decided while reading the word once.

Sort every character into one of four bins: vowel, consonant, digit, or not allowed. A single character in the last bin decides the answer immediately.

Digits are allowed but help neither the vowel rule nor the consonant rule, which is the easy detail to miss.

Approach: One pass with two flags

  1. If the word has fewer than 3 characters, return false.
  2. Set hasVowel and hasConsonant to false.
  3. For each character: if it is not a letter or digit, return false. If it is one of aeiouAEIOU, set hasVowel. If it is any other letter, set hasConsonant.
  4. Return hasVowel && hasConsonant.

Step-by-step example

word = "234Adas"
  1. Length 7 is at least 3, so the length rule passes.
  2. 2, 3, 4 are digits: allowed, but no flag changes.
  3. A is a vowel, so hasVowel = true.
  4. d is a consonant, so hasConsonant = true. a and s change nothing new.
  5. Both flags are true and no illegal character appeared, so the answer is true.

Code: Python, Java, C++

1class Solution:
2    def isValid(self, word: str) -> bool:
3        if len(word) < 3:
4            return False
5        has_vowel = has_consonant = False
6        for c in word:
7            if not c.isalnum():
8                return False
9            if c.isalpha():
10                if c.lower() in "aeiou":
11                    has_vowel = True
12                else:
13                    has_consonant = True
14        return has_vowel and has_consonant
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each character is classified once.

Space

O(1)

Only two boolean flags are kept.

Other approaches

Regular expressions (time O(n), space O(1))

Test the whole word against ^[a-zA-Z0-9]{3,}$, then search for [aeiouAEIOU] and [b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]. Compact, but three scans and harder to read.

Count by category (time O(n), space O(1))

Count vowels, consonants, digits and other characters, then check that others is 0, vowels and consonants are positive, and the total is at least 3. Same pass, without the early exit.

Common mistakes

  • Treating digits as consonants. "a12" has no consonant and is not valid.
  • Checking vowels in lowercase only. "AB1" is valid because A is a vowel.
  • Forgetting the length rule, which makes "ab" look valid.

Frequently asked questions

Is 'y' a vowel here?

No. Only a, e, i, o and u in either case are vowels, so 'y' is a consonant.

Why return as soon as an illegal character appears?

One illegal character makes the word invalid no matter what follows, so reading further cannot change the answer.

Can a word with only digits be valid?

No. It has no letters at all, so both the vowel and the consonant rules fail.