|
| 1 | +''' |
| 2 | +A word is considered valid if: |
| 3 | +
|
| 4 | +It contains a minimum of 3 characters. |
| 5 | +It contains only digits (0-9), and English letters (uppercase and lowercase). |
| 6 | +It includes at least one vowel. |
| 7 | +It includes at least one consonant. |
| 8 | +You are given a string word. |
| 9 | +
|
| 10 | +Return true if word is valid, otherwise, return false. |
| 11 | +
|
| 12 | +Notes: |
| 13 | +
|
| 14 | +'a', 'e', 'i', 'o', 'u', and their uppercases are vowels. |
| 15 | +A consonant is an English letter that is not a vowel. |
| 16 | +''' |
| 17 | + |
| 18 | + |
| 19 | +class Solution: |
| 20 | + def isValid(self, word: str) -> bool: |
| 21 | + vowels = 'aeiou' |
| 22 | + |
| 23 | + word = word.lower() |
| 24 | + cond1 = any(ch in vowels for ch in word) |
| 25 | + cond2 = any(ch not in vowels and ch.isalpha() for ch in word) |
| 26 | + |
| 27 | + cond4 = len(word)>=3 |
| 28 | + |
| 29 | + cond5 = all(ch.isdigit() or ch.isalpha() for ch in word) |
| 30 | + |
| 31 | + return cond1 and cond2 and cond4 and cond5 |
| 32 | + |
| 33 | +---------------------------------- |
| 34 | +#regex |
| 35 | + |
| 36 | +class Solution: |
| 37 | + def isValid(self, w: str) -> bool: |
| 38 | + return match('^(?=.*[aeiou])(?=.*[^0-9aeiou])[a-z0-9]{3,}$',w,I) |
| 39 | + |
| 40 | +------------------------------------------------------------------------------------ |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +class Solution: |
| 45 | + def isValid(self, word: str) -> bool: |
| 46 | + if len(word) < 3: |
| 47 | + return False |
| 48 | + |
| 49 | + vowelcount = 0 |
| 50 | + consonantcount = 0 |
| 51 | + validcharacterscount = 0 |
| 52 | + vowels = "aeiou" |
| 53 | + |
| 54 | + for char in word: |
| 55 | + if char.isalpha(): |
| 56 | + if char.lower() in vowels: |
| 57 | + vowelcount += 1 |
| 58 | + else: |
| 59 | + consonantcount += 1 |
| 60 | + validcharacterscount += 1 |
| 61 | + elif char.isdigit(): |
| 62 | + validcharacterscount += 1 |
| 63 | + else: |
| 64 | + return False |
| 65 | + |
| 66 | + # Check if it meets all criteria |
| 67 | + return validcharacterscount >= 3 and vowelcount >= 1 and consonantcount >= 1 |
0 commit comments