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.c | |
parent | 4349ad85a06f47f84abbfbdb98427083ed5ca430 (diff) | |
download | leetcode-a18969f1aacb4e46ea00cd1415ae349433fb1b70.tar.gz leetcode-a18969f1aacb4e46ea00cd1415ae349433fb1b70.zip |
3136. valid word
Diffstat (limited to 'valid_word.c')
-rw-r--r-- | valid_word.c | 38 |
1 files changed, 38 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 +} |