|
| 1 | +--- |
| 2 | +title: 3136.有效单词:遍历模拟 |
| 3 | +date: 2025-07-15 23:43:04 |
| 4 | +tags: [题解, LeetCode, 简单, 字符串, 遍历, 模拟] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】3136.有效单词:遍历模拟 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/valid-word/](https://leetcode.cn/problems/valid-word/) |
| 11 | + |
| 12 | +<p><strong>有效单词</strong> 需要满足以下几个条件:</p> |
| 13 | + |
| 14 | +<ul> |
| 15 | + <li><strong>至少 </strong>包含 3 个字符。</li> |
| 16 | + <li>由数字 0-9 和英文大小写字母组成。(不必包含所有这类字符。)</li> |
| 17 | + <li><strong>至少</strong> 包含一个 <strong>元音字母 </strong>。</li> |
| 18 | + <li><strong>至少</strong> 包含一个 <strong>辅音字母 </strong>。</li> |
| 19 | +</ul> |
| 20 | + |
| 21 | +<p>给你一个字符串 <code>word</code> 。如果 <code>word</code> 是一个有效单词,则返回 <code>true</code> ,否则返回 <code>false</code> 。</p> |
| 22 | + |
| 23 | +<p><strong>注意:</strong></p> |
| 24 | + |
| 25 | +<ul> |
| 26 | + <li><code>'a'</code>、<code>'e'</code>、<code>'i'</code>、<code>'o'</code>、<code>'u'</code> 及其大写形式都属于<strong> 元音字母 </strong>。</li> |
| 27 | + <li>英文中的 <strong>辅音字母 </strong>是指那些除元音字母之外的字母。</li> |
| 28 | +</ul> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | + |
| 32 | +<p><strong class="example">示例 1:</strong></p> |
| 33 | + |
| 34 | +<div class="example-block"> |
| 35 | +<p><strong>输入:</strong><span class="example-io">word = "234Adas"</span></p> |
| 36 | + |
| 37 | +<p><strong>输出:</strong><span class="example-io">true</span></p> |
| 38 | + |
| 39 | +<p><strong>解释:</strong></p> |
| 40 | + |
| 41 | +<p>这个单词满足所有条件。</p> |
| 42 | +</div> |
| 43 | + |
| 44 | +<p><strong class="example">示例 2:</strong></p> |
| 45 | + |
| 46 | +<div class="example-block"> |
| 47 | +<p><strong>输入:</strong><span class="example-io">word = "b3"</span></p> |
| 48 | + |
| 49 | +<p><strong>输出:</strong><span class="example-io">false</span></p> |
| 50 | + |
| 51 | +<p><strong>解释:</strong></p> |
| 52 | + |
| 53 | +<p>这个单词的长度少于 3 且没有包含元音字母。</p> |
| 54 | +</div> |
| 55 | + |
| 56 | +<p><strong class="example">示例 3:</strong></p> |
| 57 | + |
| 58 | +<div class="example-block"> |
| 59 | +<p><strong>输入:</strong><span class="example-io">word = "a3$e"</span></p> |
| 60 | + |
| 61 | +<p><strong>输出:</strong><span class="example-io">false</span></p> |
| 62 | + |
| 63 | +<p><strong>解释:</strong></p> |
| 64 | + |
| 65 | +<p>这个单词包含了 <code>'$'</code> 字符且没有包含辅音字母。</p> |
| 66 | +</div> |
| 67 | + |
| 68 | +<p> </p> |
| 69 | + |
| 70 | +<p><strong>提示:</strong></p> |
| 71 | + |
| 72 | +<ul> |
| 73 | + <li><code>1 <= word.length <= 20</code></li> |
| 74 | + <li><code>word</code> 由英文大写和小写字母、数字、<code>'@'</code>、<code>'#'</code> 和 <code>'$'</code> 组成。</li> |
| 75 | +</ul> |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +## 解题方法:遍历 |
| 80 | + |
| 81 | +如果word长度小于3,则直接返回false。 |
| 82 | + |
| 83 | +使用两个布尔类型的变量hasYuan和hasFu统计是否有元音字符和辅音字符。 |
| 84 | + |
| 85 | +遍历字符串: |
| 86 | + |
| 87 | ++ 如果当前字符是大写字母,将大写字母转为小写字母(加上32) |
| 88 | ++ 如果当前字符是小写字母(转后也算),则判断当前字符是否是元音字符 |
| 89 | + |
| 90 | + + 如果是,则将hasYuan设置为true |
| 91 | + + 否则,将hasFu设置为true |
| 92 | + |
| 93 | ++ 否则(不是字母),如果当前字符不是数字,则直接返回false |
| 94 | + |
| 95 | +最终若hasYuan和hasFu都为true则返回true。 |
| 96 | + |
| 97 | ++ 时间复杂度$O(len(word))$ |
| 98 | ++ 空间复杂度$O(1)$ |
| 99 | + |
| 100 | +### AC代码 |
| 101 | + |
| 102 | +#### C++ |
| 103 | + |
| 104 | +```cpp |
| 105 | +/* |
| 106 | + * @Author: LetMeFly |
| 107 | + * @Date: 2025-07-15 23:15:03 |
| 108 | + * @LastEditors: LetMeFly.xyz |
| 109 | + * @LastEditTime: 2025-07-15 23:22:47 |
| 110 | + */ |
| 111 | +#if defined(_WIN32) || defined(__APPLE__) |
| 112 | +#include "_[1,2]toVector.h" |
| 113 | +#endif |
| 114 | + |
| 115 | +class Solution { |
| 116 | +private: |
| 117 | + bool isYuan(char c) { |
| 118 | + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; |
| 119 | + } |
| 120 | +public: |
| 121 | + bool isValid(string word) { |
| 122 | + if (word.size() < 3) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + bool hasYuan = false, hasFu = false; |
| 126 | + for (char c : word) { |
| 127 | + if ('A' <= c && c <= 'Z') { |
| 128 | + // python -c "print(ord('a') - ord('A'))" |
| 129 | + c += 32; |
| 130 | + } |
| 131 | + if ('a' <= c && c <= 'z') { |
| 132 | + if (isYuan(c)) { |
| 133 | + hasYuan = true; |
| 134 | + } else { |
| 135 | + hasFu = true; |
| 136 | + } |
| 137 | + } else if (c < '0' || c > '9') { |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + return hasYuan && hasFu; |
| 142 | + } |
| 143 | +}; |
| 144 | +``` |
| 145 | +
|
| 146 | +#### Python |
| 147 | +
|
| 148 | +```python |
| 149 | +''' |
| 150 | +Author: LetMeFly |
| 151 | +Date: 2025-07-15 23:15:03 |
| 152 | +LastEditors: LetMeFly.xyz |
| 153 | +LastEditTime: 2025-07-15 23:30:52 |
| 154 | +''' |
| 155 | +class Solution: |
| 156 | + def isValid(self, word: str) -> bool: |
| 157 | + if len(word) < 3: |
| 158 | + return False |
| 159 | + ok = [False, False] |
| 160 | + for c in word: |
| 161 | + if c.isalpha(): |
| 162 | + ok[c.lower() in 'aeiou'] = True |
| 163 | + elif not c.isdigit(): |
| 164 | + return False |
| 165 | + return all(ok) |
| 166 | +``` |
| 167 | + |
| 168 | +#### Java |
| 169 | + |
| 170 | +```java |
| 171 | +/* |
| 172 | + * @Author: LetMeFly |
| 173 | + * @Date: 2025-07-15 23:15:03 |
| 174 | + * @LastEditors: LetMeFly.xyz |
| 175 | + * @LastEditTime: 2025-07-15 23:35:42 |
| 176 | + */ |
| 177 | +class Solution { |
| 178 | + private boolean isYuan(char c) { |
| 179 | + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; |
| 180 | + } |
| 181 | + |
| 182 | + public boolean isValid(String word) { |
| 183 | + if (word.length() < 3) { |
| 184 | + return false; |
| 185 | + } |
| 186 | + boolean hasYuan = false, hasFu = false; |
| 187 | + for (char c : word.toCharArray()) { |
| 188 | + if ('A' <= c && c <= 'Z') { |
| 189 | + c += 32; |
| 190 | + } |
| 191 | + if ('a' <= c && c <= 'z') { |
| 192 | + if (isYuan(c)) { |
| 193 | + hasYuan = true; |
| 194 | + } else { |
| 195 | + hasFu = true; |
| 196 | + } |
| 197 | + } else if (c < '0' || c > '9') { |
| 198 | + return false; |
| 199 | + } |
| 200 | + } |
| 201 | + return hasYuan && hasFu; |
| 202 | + } |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +#### Go |
| 207 | + |
| 208 | +```go |
| 209 | +/* |
| 210 | + * @Author: LetMeFly |
| 211 | + * @Date: 2025-07-15 23:15:03 |
| 212 | + * @LastEditors: LetMeFly.xyz |
| 213 | + * @LastEditTime: 2025-07-15 23:40:26 |
| 214 | + */ |
| 215 | +package main |
| 216 | + |
| 217 | +func isYuan3136(c byte) bool { |
| 218 | + return c == 'a' || c == 'e' || c == 'o' || c == 'i' || c == 'u' |
| 219 | +} |
| 220 | + |
| 221 | +func isValid(word string) bool { |
| 222 | + if len(word) < 3 { |
| 223 | + return false |
| 224 | + } |
| 225 | + hasYuan, hasFu := false, false |
| 226 | + for _, c := range word { |
| 227 | + if 'A' <= c && c <= 'Z' { |
| 228 | + c += 32 |
| 229 | + } |
| 230 | + if 'a' <= c && c <= 'z' { |
| 231 | + if isYuan3136(byte(c)) { |
| 232 | + hasYuan = true |
| 233 | + } else { |
| 234 | + hasFu = true |
| 235 | + } |
| 236 | + } else if c < '0' || c > '9' { |
| 237 | + return false |
| 238 | + } |
| 239 | + } |
| 240 | + return hasYuan && hasFu |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/149373884)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/07/15/LeetCode%203136.%E6%9C%89%E6%95%88%E5%8D%95%E8%AF%8D/)哦~ |
| 245 | +> |
| 246 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments