-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3136-ValidWord.go
More file actions
85 lines (73 loc) · 2.59 KB
/
3136-ValidWord.go
File metadata and controls
85 lines (73 loc) · 2.59 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
package main
// 3136. Valid Word
// A word is considered valid if:
// 1. It contains a minimum of 3 characters.
// 2. It contains only digits (0-9), and English letters (uppercase and lowercase).
// 3. It includes at least one vowel.
// 4. It includes at least one consonant.
// You are given a string word.
// Return true if word is valid, otherwise, return false.
// Notes:
// 1. 'a', 'e', 'i', 'o', 'u', and their uppercases are vowels.
// 2. 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 '$'.
import "fmt"
func isValid(word string) bool {
if len(word) < 3 { return false } // 1. It contains a minimum of 3 characters.
vowels, consonant := 0, 0
isVowel := func(ch byte) bool {
return 'a' == ch || 'e' == ch || 'i' == ch || 'o' == ch || 'u' == ch ||
'A' == ch || 'E' == ch || 'I' == ch || 'O' == ch || 'U' == ch
}
for i := range word {
if word[i] == '@' || word[i] == '#' || word[i] == '$' { return false } // 2. It contains only digits (0-9), and English letters (uppercase and lowercase).
if isVowel(word[i]) {
vowels++
} else if (word[i] >= 'a' && word[i] <='z' ) || (word[i] >= 'A' && word[i] <= 'Z') {
consonant++
}
}
// 3. It includes at least one vowel.
// 4. It includes at least one consonant.
return vowels > 0 && consonant > 0
}
func main() {
// Example 1:
// Input: word = "234Adas"
// Output: true
// Explanation:
// This word satisfies the conditions.
fmt.Println(isValid("234Adas")) // true
// Example 2:
// Input: word = "b3"
// Output: false
// Explanation:
// The length of this word is fewer than 3, and does not have a vowel.
fmt.Println(isValid("b3")) // false
// Example 3:
// Input: word = "a3$e"
// Output: false
// Explanation:
// This word contains a '$' character and does not have a consonant.
fmt.Println(isValid("a3$e")) // false
fmt.Println(isValid("aaaa")) // false
fmt.Println(isValid("bluefrog")) // true
fmt.Println(isValid("leetcode")) // true
}