-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (79 loc) · 1.97 KB
/
main.go
File metadata and controls
85 lines (79 loc) · 1.97 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
// Source: https://leetcode.com/problems/valid-word
// Title: Valid Word
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 `'$'`.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"unicode"
)
func isValid(word string) bool {
if len(word) < 3 {
return false
}
hasVowel := false
hasConsonant := false
for _, ch := range word {
if unicode.IsLetter(ch) {
ch = unicode.ToLower(ch)
if ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' {
hasVowel = true
} else {
hasConsonant = true
}
} else if unicode.IsDigit(ch) {
} else {
return false
}
}
return hasVowel && hasConsonant
}