less than 1 minute read

Problem Statement

leetcode problem link

Brute Force [Accepted]


class Solution:
    def isValid(self, word: str) -> bool:
        MIN_LEN = 3
        if len(word) < MIN_LEN:
            return False
        vowels = {'a','e','i','o','u'}
        hasVowel = hasConsonant = False
        for c in word:
            ch = c.lower()
            if not ch.isalnum():
                return False
            if ch in vowels:
                hasVowel = True
            if not ch.isdigit() and ch not in vowels:
                hasConsonant = True
        return hasVowel and hasConsonant

Editorial

Approach: One-Time Traversal

class Solution:
    def isValid(self, word: str) -> bool:
        if len(word) < 3:
            return False

        has_vowel = False
        has_consonant = False

        for c in word:
            if c.isalpha():
                if c.lower() in "aeiou":
                    has_vowel = True
                else:
                    has_consonant = True
            elif not c.isdigit():
                return False

        return has_vowel and has_consonant