Skip to content

Commit 3607f07

Browse files
authored
update: 添加问题“3136.有效单词”的代码和题解(#1029)
* 3136: WA.cpp (#1028) * 3136: dbg.cpp (#1028) * 3136: AC.cpp+RE.py (#1028) cpp - AC,100.00%,89.16% * 3136: WA.py (#1028) "AhI" "IMG" * 3136: AC.py (#1028) py - AC,27.85%,25.32% * 3136: CE.java (#1028) Line 17: error: for-each not applicable to expression type for (char c : word) { ^ required: array or java.lang.Iterable found: String * 3136: AC.java (#1028) AC,93.10%,84.48% * 3136: AC.go (#1028) AC,100.00%,30.77% * update: 添加问题“3136.有效单词”的代码和题解(#1029) Mac上还是未处理那个commit 今日mac此仓库进度为0 Signed-off-by: LetMeFly666 <814114971@qq.com> --------- Signed-off-by: LetMeFly666 <814114971@qq.com>
1 parent 32eb954 commit 3607f07

File tree

9 files changed

+382
-2
lines changed

9 files changed

+382
-2
lines changed

.commitmsg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Mac上还是未处理那个commit
2+
3+
今日mac此仓库进度为0

.gitlog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git branch -d dev
2+
error: The branch 'dev' is not fully merged.
3+
If you are sure you want to delete it, run 'git branch -D dev'.
4+
PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git branch -D dev
5+
Deleted branch dev (was c2a5f471375).
6+
PS F:\OtherApps\Program\Git\Store\Store20_LeetCode>

Codes/3136-valid-word.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-15 23:15:03
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-15 23:22:47
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
bool isYuan(char c) {
14+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
15+
}
16+
public:
17+
bool isValid(string word) {
18+
if (word.size() < 3) {
19+
return false;
20+
}
21+
bool hasYuan = false, hasFu = false;
22+
for (char c : word) {
23+
if ('A' <= c && c <= 'Z') {
24+
// python -c "print(ord('a') - ord('A'))"
25+
c += 32;
26+
}
27+
if ('a' <= c && c <= 'z') {
28+
if (isYuan(c)) {
29+
hasYuan = true;
30+
} else {
31+
hasFu = true;
32+
}
33+
} else if (c < '0' || c > '9') {
34+
return false;
35+
}
36+
}
37+
return hasYuan && hasFu;
38+
}
39+
};

Codes/3136-valid-word.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-15 23:15:03
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-15 23:40:26
6+
*/
7+
package main
8+
9+
func isYuan3136(c byte) bool {
10+
return c == 'a' || c == 'e' || c == 'o' || c == 'i' || c == 'u'
11+
}
12+
13+
func isValid(word string) bool {
14+
if len(word) < 3 {
15+
return false
16+
}
17+
hasYuan, hasFu := false, false
18+
for _, c := range word {
19+
if 'A' <= c && c <= 'Z' {
20+
c += 32
21+
}
22+
if 'a' <= c && c <= 'z' {
23+
if isYuan3136(byte(c)) {
24+
hasYuan = true
25+
} else {
26+
hasFu = true
27+
}
28+
} else if c < '0' || c > '9' {
29+
return false
30+
}
31+
}
32+
return hasYuan && hasFu
33+
}

Codes/3136-valid-word.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-15 23:15:03
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-15 23:35:42
6+
*/
7+
class Solution {
8+
private boolean isYuan(char c) {
9+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
10+
}
11+
12+
public boolean isValid(String word) {
13+
if (word.length() < 3) {
14+
return false;
15+
}
16+
boolean hasYuan = false, hasFu = false;
17+
for (char c : word.toCharArray()) {
18+
if ('A' <= c && c <= 'Z') {
19+
c += 32;
20+
}
21+
if ('a' <= c && c <= 'z') {
22+
if (isYuan(c)) {
23+
hasYuan = true;
24+
} else {
25+
hasFu = true;
26+
}
27+
} else if (c < '0' || c > '9') {
28+
return false;
29+
}
30+
}
31+
return hasYuan && hasFu;
32+
}
33+
}

Codes/3136-valid-word.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-07-15 23:15:03
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-07-15 23:30:52
6+
'''
7+
class Solution:
8+
def isValid(self, word: str) -> bool:
9+
if len(word) < 3:
10+
return False
11+
ok = [False, False]
12+
for c in word:
13+
if c.isalpha():
14+
ok[c.lower() in 'aeiou'] = True
15+
elif not c.isdigit():
16+
return False
17+
return all(ok)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@
927927
|3131.找出与数组相加的整数I|简单|<a href="https://leetcode.cn/problems/find-the-integer-added-to-array-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/08/08/LeetCode%203131.%E6%89%BE%E5%87%BA%E4%B8%8E%E6%95%B0%E7%BB%84%E7%9B%B8%E5%8A%A0%E7%9A%84%E6%95%B4%E6%95%B0I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141037760" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-integer-added-to-array-i/solutions/2873708/letmefly-3131zhao-chu-yu-shu-zu-xiang-ji-mfd3/" target="_blank">LeetCode题解</a>|
928928
|3132.找出与数组相加的整数II|中等|<a href="https://leetcode.cn/problems/find-the-integer-added-to-array-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/08/09/LeetCode%203132.%E6%89%BE%E5%87%BA%E4%B8%8E%E6%95%B0%E7%BB%84%E7%9B%B8%E5%8A%A0%E7%9A%84%E6%95%B4%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141072842" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-integer-added-to-array-ii/solutions/2874942/letmefly-3132zhao-chu-yu-shu-zu-xiang-ji-eljs/" target="_blank">LeetCode题解</a>|
929929
|3133.数组最后一个元素的最小值|中等|<a href="https://leetcode.cn/problems/minimum-array-end/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/08/22/LeetCode%203133.%E6%95%B0%E7%BB%84%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E7%9A%84%E6%9C%80%E5%B0%8F%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141440484" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-array-end/solutions/2889869/letmefly-3133shu-zu-zui-hou-yi-ge-yuan-s-nh55/" target="_blank">LeetCode题解</a>|
930+
|3136.有效单词|简单|<a href="https://leetcode.cn/problems/valid-word/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/07/15/LeetCode%203136.%E6%9C%89%E6%95%88%E5%8D%95%E8%AF%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149373884" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/valid-word/solutions/3724870/letmefly-3136you-xiao-dan-ci-bian-li-mo-j8f9d/" target="_blank">LeetCode题解</a>|
930931
|3137.K周期字符串需要的最少操作次数|中等|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-word-k-periodic/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/08/17/LeetCode%203137.K%E5%91%A8%E6%9C%9F%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%9C%80%E8%A6%81%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141284611" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-word-k-periodic/solutions/2883701/letmefly-3137k-zhou-qi-zi-fu-chuan-xu-ya-uma3/" target="_blank">LeetCode题解</a>|
931932
|3138.同位字符串连接的最小长度|中等|<a href="https://leetcode.cn/problems/minimum-length-of-anagram-concatenation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/20/LeetCode%203138.%E5%90%8C%E4%BD%8D%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5%E7%9A%84%E6%9C%80%E5%B0%8F%E9%95%BF%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144620449" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-length-of-anagram-concatenation/solutions/3025912/letmefly-3138tong-wei-zi-fu-chuan-lian-j-k2tu/" target="_blank">LeetCode题解</a>|
932933
|3142.判断矩阵是否满足条件|简单|<a href="https://leetcode.cn/problems/check-if-grid-satisfies-conditions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/08/29/LeetCode%203142.%E5%88%A4%E6%96%AD%E7%9F%A9%E9%98%B5%E6%98%AF%E5%90%A6%E6%BB%A1%E8%B6%B3%E6%9D%A1%E4%BB%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141691365" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-grid-satisfies-conditions/solutions/2897475/letmefly-3142pan-duan-ju-zhen-shi-fou-ma-dald/" target="_blank">LeetCode题解</a>|
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
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>&nbsp;</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>&nbsp;</p>
69+
70+
<p><strong>提示:</strong></p>
71+
72+
<ul>
73+
<li><code>1 &lt;= word.length &lt;= 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

Comments
 (0)