|
| 1 | +# [3136. 有效单词](https://leetcode.cn/problems/valid-word) |
| 2 | + |
| 3 | +[English Version](/solution/3100-3199/3136.Valid%20Word/README_EN.md) |
| 4 | + |
| 5 | +<!-- tags: --> |
| 6 | + |
| 7 | +## 题目描述 |
| 8 | + |
| 9 | +<!-- 这里写题目描述 --> |
| 10 | + |
| 11 | +<p><strong>有效单词</strong> 需要满足以下几个条件:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li><strong>至少 </strong>包含 3 个字符。</li> |
| 15 | + <li>由数字 0-9 和英文大小写字母组成。(不必包含所有这类字符。)</li> |
| 16 | + <li><strong>至少</strong> 包含一个 <strong>元音字母 </strong>。</li> |
| 17 | + <li><strong>至少</strong> 包含一个 <strong>辅音字母 </strong>。</li> |
| 18 | +</ul> |
| 19 | + |
| 20 | +<p>给你一个字符串 <code>word</code> 。如果 <code>word</code> 是一个有效单词,则返回 <code>true</code> ,否则返回 <code>false</code> 。</p> |
| 21 | + |
| 22 | +<p><strong>注意:</strong></p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li><code>'a'</code>、<code>'e'</code>、<code>'i'</code>、<code>'o'</code>、<code>'u'</code> 及其大写形式都属于<strong> 元音字母 </strong>。</li> |
| 26 | + <li>英文中的 <strong>辅音字母 </strong>是指那些除元音字母之外的字母。</li> |
| 27 | +</ul> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | + |
| 31 | +<p><strong class="example">示例 1:</strong></p> |
| 32 | + |
| 33 | +<div class="example-block"> |
| 34 | +<p><strong>输入:</strong><span class="example-io">word = "234Adas"</span></p> |
| 35 | + |
| 36 | +<p><strong>输出:</strong><span class="example-io">true</span></p> |
| 37 | + |
| 38 | +<p><strong>解释:</strong></p> |
| 39 | + |
| 40 | +<p>这个单词满足所有条件。</p> |
| 41 | +</div> |
| 42 | + |
| 43 | +<p><strong class="example">示例 2:</strong></p> |
| 44 | + |
| 45 | +<div class="example-block"> |
| 46 | +<p><strong>输入:</strong><span class="example-io">word = "b3"</span></p> |
| 47 | + |
| 48 | +<p><strong>输出:</strong><span class="example-io">false</span></p> |
| 49 | + |
| 50 | +<p><strong>解释:</strong></p> |
| 51 | + |
| 52 | +<p>这个单词的长度少于 3 且没有包含元音字母。</p> |
| 53 | +</div> |
| 54 | + |
| 55 | +<p><strong class="example">示例 3:</strong></p> |
| 56 | + |
| 57 | +<div class="example-block"> |
| 58 | +<p><strong>输入:</strong><span class="example-io">word = "a3$e"</span></p> |
| 59 | + |
| 60 | +<p><strong>输出:</strong><span class="example-io">false</span></p> |
| 61 | + |
| 62 | +<p><strong>解释:</strong></p> |
| 63 | + |
| 64 | +<p>这个单词包含了 <code>'$'</code> 字符且没有包含辅音字母。</p> |
| 65 | +</div> |
| 66 | + |
| 67 | +<p> </p> |
| 68 | + |
| 69 | +<p><strong>提示:</strong></p> |
| 70 | + |
| 71 | +<ul> |
| 72 | + <li><code>1 <= word.length <= 20</code></li> |
| 73 | + <li><code>word</code> 由英文大写和小写字母、数字、<code>'@'</code>、<code>'#'</code> 和 <code>'$'</code> 组成。</li> |
| 74 | +</ul> |
| 75 | + |
| 76 | +## 解法 |
| 77 | + |
| 78 | +### 方法一:模拟 |
| 79 | + |
| 80 | +我们首先判断字符串的长度是否小于 3,如果是则返回 `false`。 |
| 81 | + |
| 82 | +然后我们遍历字符串,判断每个字符是否是字母或数字,如果不是则返回 `false`。否则我们判断该字符是否是元音字母,如果是则将 `has_vowel` 置为 `true`,否则将 `has_consonant` 置为 `true`。 |
| 83 | + |
| 84 | +最后,如果 `has_vowel` 和 `has_consonant` 都为 `true`,则返回 `true`,否则返回 `false`。 |
| 85 | + |
| 86 | +时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串的长度。 |
| 87 | + |
| 88 | +<!-- tabs:start --> |
| 89 | + |
| 90 | +```python |
| 91 | +class Solution: |
| 92 | + def isValid(self, word: str) -> bool: |
| 93 | + if len(word) < 3: |
| 94 | + return False |
| 95 | + has_vowel = has_consonant = False |
| 96 | + vs = set("aeiouAEIOU") |
| 97 | + for c in word: |
| 98 | + if not c.isalnum(): |
| 99 | + return False |
| 100 | + if c.isalpha(): |
| 101 | + if c in vs: |
| 102 | + has_vowel = True |
| 103 | + else: |
| 104 | + has_consonant = True |
| 105 | + return has_vowel and has_consonant |
| 106 | +``` |
| 107 | + |
| 108 | +```java |
| 109 | +class Solution { |
| 110 | + public boolean isValid(String word) { |
| 111 | + if (word.length() < 3) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + boolean hasVowel = false, hasConsonant = false; |
| 115 | + boolean[] vs = new boolean[26]; |
| 116 | + for (char c : "aeiou".toCharArray()) { |
| 117 | + vs[c - 'a'] = true; |
| 118 | + } |
| 119 | + for (char c : word.toCharArray()) { |
| 120 | + if (Character.isAlphabetic(c)) { |
| 121 | + if (vs[Character.toLowerCase(c) - 'a']) { |
| 122 | + hasVowel = true; |
| 123 | + } else { |
| 124 | + hasConsonant = true; |
| 125 | + } |
| 126 | + } else if (!Character.isDigit(c)) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + } |
| 130 | + return hasVowel && hasConsonant; |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +```cpp |
| 136 | +class Solution { |
| 137 | +public: |
| 138 | + bool isValid(string word) { |
| 139 | + if (word.size() < 3) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + bool has_vowel = false, has_consonant = false; |
| 143 | + bool vs[26]{}; |
| 144 | + string vowels = "aeiou"; |
| 145 | + for (char c : vowels) { |
| 146 | + vs[c - 'a'] = true; |
| 147 | + } |
| 148 | + for (char c : word) { |
| 149 | + if (isalpha(c)) { |
| 150 | + if (vs[tolower(c) - 'a']) { |
| 151 | + has_vowel = true; |
| 152 | + } else { |
| 153 | + has_consonant = true; |
| 154 | + } |
| 155 | + } else if (!isdigit(c)) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + } |
| 159 | + return has_vowel && has_consonant; |
| 160 | + } |
| 161 | +}; |
| 162 | +``` |
| 163 | +
|
| 164 | +```go |
| 165 | +func isValid(word string) bool { |
| 166 | + if len(word) < 3 { |
| 167 | + return false |
| 168 | + } |
| 169 | + hasVowel := false |
| 170 | + hasConsonant := false |
| 171 | + vs := make([]bool, 26) |
| 172 | + for _, c := range "aeiou" { |
| 173 | + vs[c-'a'] = true |
| 174 | + } |
| 175 | + for _, c := range word { |
| 176 | + if unicode.IsLetter(c) { |
| 177 | + if vs[unicode.ToLower(c)-'a'] { |
| 178 | + hasVowel = true |
| 179 | + } else { |
| 180 | + hasConsonant = true |
| 181 | + } |
| 182 | + } else if !unicode.IsDigit(c) { |
| 183 | + return false |
| 184 | + } |
| 185 | + } |
| 186 | + return hasVowel && hasConsonant |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +```ts |
| 191 | +function isValid(word: string): boolean { |
| 192 | + if (word.length < 3) { |
| 193 | + return false; |
| 194 | + } |
| 195 | + let hasVowel: boolean = false; |
| 196 | + let hasConsonant: boolean = false; |
| 197 | + const vowels: Set<string> = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']); |
| 198 | + for (const c of word) { |
| 199 | + if (!c.match(/[a-zA-Z0-9]/)) { |
| 200 | + return false; |
| 201 | + } |
| 202 | + if (/[a-zA-Z]/.test(c)) { |
| 203 | + if (vowels.has(c)) { |
| 204 | + hasVowel = true; |
| 205 | + } else { |
| 206 | + hasConsonant = true; |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + return hasVowel && hasConsonant; |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +<!-- tabs:end --> |
| 215 | + |
| 216 | +<!-- end --> |
0 commit comments