Skip to content

Latest commit

 

History

History
93 lines (73 loc) · 1.86 KB

_3136. Valid Word.md

File metadata and controls

93 lines (73 loc) · 1.86 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 03, 2024

Last updated : July 03, 2024


Related Topics : String

Acceptance Rate : 38.22 %


Solutions

C

bool isValid(char* word) {
    bool cons = false;
    bool vow = false;
    int len = 0;

    while (*word) {
        len++;
        if (*word >= 65 && *word <= 90) {
            *word += 32;
        }

        // letter
        if (*word >= 97 && *word <= 122) {
            switch (*word) {
                case 'a' :
                case 'e' :
                case 'i' :
                case 'o' :
                case 'u' :
                    vow = true;
                    break;
                default :
                    cons = true;
                    break;
            }
            word++;
            continue;
        }

        if (*word >= 48 && *word <= 57) {
            word++;
            continue;
        }

        return false;
    }

    return cons && vow && (len >= 3);
}

Python

class Solution:
    def isValid(self, word: str) -> bool:
        if not word.isalnum() :
            return False

        if len(word) < 3 :
            return False

        cons, vow = False, False
        vowels = set('aeiou')

        for i, c in enumerate(word) :
            if c.lower() in vowels :
                vow = True
            elif not c.isnumeric() :
                cons = True
            
            if vow and cons :
                return True
        return False