Difficulty: Easy | Acceptance: 50.90% | Paid: No Topics: String
A string word is valid if the following conditions are met:
- It contains at least 3 characters.
- It consists only of lowercase English letters.
- It contains at least one vowel (‘a’, ‘e’, ‘i’, ‘o’, ‘u’).
- It contains at least one consonant (any letter that is not a vowel).
Given a string word, return true if word is valid, otherwise return false.
- Examples
- Constraints
- Simple Iteration
- Regular Expression
- Set-Based Approach
Examples
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 <= 20
word consists of lowercase English letters and digits.
Simple Iteration
Intuition Iterate through each character of the word, checking all conditions: minimum length, valid characters only, presence of at least one vowel and one consonant.
Steps
- Check if word length is at least 3
- Initialize flags for vowel and consonant presence
- Iterate through each character:
- Return false if character is not a lowercase letter
- Update vowel or consonant flag based on character
- Return true only if both flags are true
class Solution:
def isValid(self, word: str) -> bool:
if len(word) < 3:
return False
vowels = set('aeiou')
has_vowel = False
has_consonant = False
for c in word:
if not c.isalpha() or not c.islower():
return False
if c in vowels:
has_vowel = True
else:
has_consonant = True
return has_vowel and has_consonant
Complexity
- Time: O(n) where n is the length of the word
- Space: O(1) - only using a fixed-size set for vowels
- Notes: Most straightforward approach with clear logic flow
Regular Expression
Intuition Use regex patterns to validate all conditions in a concise manner by matching against specific patterns.
Steps
- Check minimum length condition separately
- Create regex pattern for valid characters (lowercase letters only)
- Create regex pattern for at least one vowel
- Create regex pattern for at least one consonant
- All conditions must be satisfied
import re
class Solution:
def isValid(self, word: str) -> bool:
if len(word) < 3:
return False
# Check all characters are lowercase letters
if not re.fullmatch(r'[a-z]+', word):
return False
# Check at least one vowel
if not re.search(r'[aeiou]', word):
return False
# Check at least one consonant
if not re.search(r'[^aeiou]', word):
return False
return True
Complexity
- Time: O(n) - regex engine scans the string
- Space: O(1) - constant space for regex patterns
- Notes: More concise but may have overhead from regex compilation
Set-Based Approach
Intuition Use set operations to efficiently check for vowel and consonant presence while validating character constraints.
Steps
- Check minimum length requirement
- Create a set of all vowels
- Create sets for found vowels and consonants
- Iterate through word, populating respective sets
- Validate all conditions using set operations
class Solution:
def isValid(self, word: str) -> bool:
if len(word) < 3:
return False
vowels = set('aeiou')
found_vowels = set()
found_consonants = set()
for c in word:
if not c.isalpha() or not c.islower():
return False
if c in vowels:
found_vowels.add(c)
else:
found_consonants.add(c)
return len(found_vowels) > 0 and len(found_consonants) > 0
Complexity
- Time: O(n) - single pass through the string
- Space: O(1) - sets are bounded by alphabet size (26 letters)
- Notes: Uses more memory than simple iteration but provides clear separation of concerns