-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3136.valid-word.cpp
More file actions
61 lines (61 loc) · 1.21 KB
/
3136.valid-word.cpp
File metadata and controls
61 lines (61 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* @lc app=leetcode id=3136 lang=cpp
*
* [3136] Valid Word
*/
#include <string>
using namespace std;
// @lc code=start
class Solution
{
public:
bool VOWEL[128];
char vowel[10] = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
Solution()
{
for (auto c : vowel)
{
VOWEL[c] = true;
}
}
bool is_vowel(char c)
{
return VOWEL[c];
}
bool is_consnant(char c)
{
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) && !VOWEL[c];
}
bool is_digit(char c)
{
return c >= '0' && c <= '9';
}
bool isValid(string word)
{
if (word.length() < 3)
return false;
bool has_vowel = false;
bool has_consonant = false;
for (auto c : word)
{
if (is_vowel(c))
{
has_vowel = true;
}
else if (is_consnant(c))
{
has_consonant = true;
}
else if (is_digit(c))
{
continue;
}
else
{
return false;
}
}
return has_vowel && has_consonant;
}
};
// @lc code=end