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 | |
parent | 4349ad85a06f47f84abbfbdb98427083ed5ca430 (diff) | |
download | leetcode-master.tar.gz leetcode-master.zip |
-rw-r--r-- | valid_word.c | 38 | ||||
-rw-r--r-- | valid_word.py | 40 |
2 files changed, 78 insertions, 0 deletions
diff --git a/valid_word.c b/valid_word.c new file mode 100644 index 0000000..965e5b1 --- /dev/null +++ b/valid_word.c @@ -0,0 +1,38 @@ +// 3136. Valid Word +#include "leetcode.h" + +/* + * 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. + */ + +bool isvowel(char c) { + return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || + c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); +} + +bool isValid(char *word) { + bool vowel = false, consonant = false, invalid = false; + for (int i = 0; word[i]; i++) + if (isalnum(word[i])) { + if (isalpha(word[i])) { + if (isvowel(word[i])) + vowel = true; + else + consonant = true; + } + } else { + invalid = true; + break; + } + return (strlen(word) > 2 && vowel && consonant && !invalid); +} + +int main() { + char *w1 = "234Adas", *w2 = "b3", *w3 = "a3$e"; + printf("%d\n", isValid(w1)); // expect: 1 + printf("%d\n", isValid(w2)); // expect: 0 + printf("%d\n", isValid(w3)); // expect: 0 +} 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")) |