Problem Description
Given a string word, determine if it is a valid word. A valid word must have a minimum length of 3, consist solely of digits and English letters (both uppercase and lowercase), and must contain at least one vowel and at least one consonant (for letters only). If the word meets all conditions, return true; otherwise, return false.
Key Insights
- Check the length of the word; if less than 3, it's immediately invalid.
- Ensure every character is either a digit or an English letter.
- To validate vowels and consonants, iterate over each letter and use a set for vowels.
- For letters, if it is not a vowel, it is a consonant.
- Digits do not affect vowel/consonant conditions but are allowed in the word.
Space and Time Complexity
Time Complexity: O(n) where n is the number of characters in the word.
Space Complexity: O(1) constant space usage.
Solution
The approach iterates through the input string once. During the iteration, it validates that each character is either a digit or a letter. For letters, the solution determines if the character is a vowel (using a predefined set of vowels) or a consonant. The function tracks the presence of at least one vowel and one consonant. If the word's length is less than 3 or if it contains invalid characters, the solution returns false. Otherwise, based on the tracked vowel and consonant, it returns whether the word is valid. The main data structures used are a set (or hash set) for vowels and simple boolean flags to capture required conditions.