diff options
author | Jack Sangdahl <[email protected]> | 2025-07-15 00:31:45 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-07-15 00:31:45 -0600 |
commit | a18969f1aacb4e46ea00cd1415ae349433fb1b70 (patch) | |
tree | 48da6e87a319ab64af23abf39f450245cc14b0f3 /valid_word.py | |
parent | 4349ad85a06f47f84abbfbdb98427083ed5ca430 (diff) | |
download | leetcode-a18969f1aacb4e46ea00cd1415ae349433fb1b70.tar.gz leetcode-a18969f1aacb4e46ea00cd1415ae349433fb1b70.zip |
3136. valid word
Diffstat (limited to 'valid_word.py')
-rw-r--r-- | valid_word.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/valid_word.py b/valid_word.py new file mode 100644 index 0000000..8728305 --- /dev/null +++ b/valid_word.py @@ -0,0 +1,40 @@ +# 3136. Valid Word + +""" +a word is considered valid if it contains a minimum of three characters, it +contains only digits (0-9), and english letters (uppercase and lowercase), it +includes at least one vowel, and it includes one consonant. you are given a +string 'word'. return true if 'word' is valid, otherwise return false. +""" + + +class Solution(object): + def isValid(self, word): + """ + :type word: str + :rtype: bool + """ + n, m = len(word), 0 + if n < 3: + return False + vowels = ( + 1 + | (1 << (ord("e") - 97)) + | (1 << (ord("o") - 97)) + | (1 << (ord("i") - 97)) + | (1 << (ord("u") - 97)) + ) + for i in word: + if not (i.isalpha() or i.isdigit()): + return False + if i.isalpha(): + j = ord(i) - 97 if ord(i) >= 97 else ord(i) - 65 + m |= 1 << (vowels >> j) & 1 + return m == 3 + + +if __name__ == "__main__": + obj = Solution() + print(obj.isValid(word="234Adas")) + print(obj.isValid(word="b3")) + print(obj.isValid(word="a3$e")) |