-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path15_Valid_Word.cpp
More file actions
108 lines (76 loc) · 2.28 KB
/
Copy path15_Valid_Word.cpp
File metadata and controls
108 lines (76 loc) · 2.28 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// 3136. Valid Word
// A word is considered valid if:
// It contains a minimum of 3 characters.
// It contains only digits (0-9), and English letters (uppercase and lowercase).
// It includes at least one vowel.
// It includes at least one consonant.
// You are given a string word.
// Return true if word is valid, otherwise, return false.
// Notes:
// 'a', 'e', 'i', 'o', 'u', and their uppercases are vowels.
// A consonant is an English letter that is not a vowel.
// Example 1:
// Input: word = "234Adas"
// Output: true
// Explanation:
// This word satisfies the conditions.
// Example 2:
// Input: word = "b3"
// Output: false
// Explanation:
// The length of this word is fewer than 3, and does not have a vowel.
// Example 3:
// Input: word = "a3$e"
// Output: false
// Explanation:
// This word contains a '$' character and does not have a consonant.
// Constraints:
// 1 <= word.length <= 20
// word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.
class Solution
{
public:
bool isValid(string s)
{
int n = s.length();
if (n < 3)
return false;
int vowels = 0, consonants = 0;
string vowelList = "aeiouAEIOU";
for (char c : s)
{
if (isalpha(c))
{
if (vowelList.find(c) != string::npos)
{
vowels++;
}
else
{
consonants++;
}
}
else if (!isdigit(c))
{
return false;
}
}
return vowels >= 1 && consonants >= 1;
}
};
/*
This code checks if a given string is valid according to the following rules:
1. Length must be at least 3 characters
2. Can only contain letters and digits
3. Must have at least one vowel
4. Must have at least one consonant
The algorithm:
1. First checks string length
2. Maintains counters for vowels and consonants
3. Iterates through each character:
- If it's a letter, checks if it's a vowel or consonant
- If it's not a letter or digit, returns false
4. Finally checks if both vowels and consonants are present
Time Complexity: O(n) where n is the length of string
Space Complexity: O(1) as we only use fixed extra space
*/