A word is considered valid if:
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.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 <= 20word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you need to figure out if a given word meets a specific set of rules to be considered valid. The brute force approach involves looking at the word as a whole and individually checking every single rule against it one by one.
Here's how the algorithm would work step-by-step:
def is_valid(word):
# We enforce a minimum length because short words lack enough structural complexity.
if len(word) < 3:
return False
for character in word:
if not character.isalnum():
return False
has_vowel_flag = False
for character in word:
if character.lower() in 'aeiou':
has_vowel_flag = True
# Vowels are mandatory to ensure the word contains pronounceable syllabic sounds.
if not has_vowel_flag:
return False
has_consonant_flag = False
for character in word:
if character.isalpha() and character.lower() not in 'aeiou':
has_consonant_flag = True
# Consonants are necessary alongside vowels to form a complete valid word structure.
if not has_consonant_flag:
return False
return TrueTo determine if a word is valid, we simply need to inspect it against a few basic rules regarding its length and the types of characters it contains. We can check these rules one by one to see if the word passes all of them.
Here's how the algorithm would work step-by-step:
def is_valid(word_to_check):
# The minimum length constraint ensures words are not too short to be meaningful.
if len(word_to_check) < 3:
return False
has_vowel = False
has_consonant = False
vowels_set = set("aeiouAEIOU")
for character in word_to_check:
if not character.isalnum():
return False
if character.isalpha():
if character in vowels_set:
has_vowel = True
else:
has_consonant = True
# Both vowels and consonants are required to form a linguistically valid token.
return has_vowel and has_consonant| Case | How to Handle |
|---|---|
| Empty or null string input | Check for null or empty strings at the start and return false immediately. |
| String length is less than the minimum required character count | Check the length of the string against the minimum constraint and return false if it falls short. |
| String contains only vowels with no consonants | Maintain a boolean flag for consonants and return false if it remains unset after a full scan. |
| String contains only consonants with no vowels | Maintain a boolean flag for vowels and return false if it remains unset after checking all characters. |
| String contains invalid characters such as punctuation or spaces | Validate each character against an allowed set of alphanumeric characters and reject if any foreign symbol appears. |
| Mixed case letters for vowels and consonants | Convert the entire string to lowercase during preprocessing to simplify vowel and consonant identification. |
| Extremely large string inputs | Iterate through the string in a single linear pass with O(n) time complexity and O(1) space complexity to ensure optimal performance. |