ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

LeetCode 3136.有效单词:遍历模拟

2026/8/6 5:38:59 拓冰建站 浏览量
LeetCode 3136.有效单词:遍历模拟

【LetMeFly】3136.有效单词:遍历模拟

力扣题目链接:https://leetcode.cn/problems/valid-word/

有效单词 需要满足以下几个条件:

  • 至少 包含 3 个字符。
  • 由数字 0-9 和英文大小写字母组成。(不必包含所有这类字符。)
  • 至少 包含一个 元音字母
  • 至少 包含一个 辅音字母

给你一个字符串 word 。如果 word 是一个有效单词,则返回 true ,否则返回 false

注意:

  • 'a''e''i''o''u' 及其大写形式都属于 元音字母
  • 英文中的 辅音字母 是指那些除元音字母之外的字母。

 

示例 1:

输入:word = "234Adas"

输出:true

解释:

这个单词满足所有条件。

示例 2:

输入:word = "b3"

输出:false

解释:

这个单词的长度少于 3 且没有包含元音字母。

示例 3:

输入:word = "a3$e"

输出:false

解释:

这个单词包含了 '$' 字符且没有包含辅音字母。

 

提示:

  • 1 <= word.length <= 20
  • word 由英文大写和小写字母、数字、'@''#''$' 组成。

解题方法:遍历

如果word长度小于3,则直接返回false。

使用两个布尔类型的变量hasYuan和hasFu统计是否有元音字符和辅音字符。

遍历字符串:

  • 如果当前字符是大写字母,将大写字母转为小写字母(加上32)

  • 如果当前字符是小写字母(转后也算),则判断当前字符是否是元音字符

    • 如果是,则将hasYuan设置为true
    • 否则,将hasFu设置为true
  • 否则(不是字母),如果当前字符不是数字,则直接返回false

最终若hasYuan和hasFu都为true则返回true。

  • 时间复杂度O(len(word))O(len(word))O(len(word))
  • 空间复杂度O(1)O(1)O(1)

AC代码

C++
/** @Author: LetMeFly* @Date: 2025-07-15 23:15:03* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-07-15 23:22:47*/
#if defined(_WIN32) || defined(__APPLE__)
#include "_[1,2]toVector.h"
#endifclass Solution {
private:bool isYuan(char c) {return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';}
public:bool isValid(string word) {if (word.size() < 3) {return false;}bool hasYuan = false, hasFu = false;for (char c : word) {if ('A' <= c && c <= 'Z') {// python -c "print(ord('a') - ord('A'))"c += 32;}if ('a' <= c && c <= 'z') {if (isYuan(c)) {hasYuan = true;} else {hasFu = true;}} else if (c < '0' || c > '9') {return false;}}return hasYuan && hasFu;}
};
Python
'''
Author: LetMeFly
Date: 2025-07-15 23:15:03
LastEditors: LetMeFly.xyz
LastEditTime: 2025-07-15 23:30:52
'''
class Solution:def isValid(self, word: str) -> bool:if len(word) < 3:return Falseok = [False, False]for c in word:if c.isalpha():ok[c.lower() in 'aeiou'] = Trueelif not c.isdigit():return Falsereturn all(ok)
Java
/** @Author: LetMeFly* @Date: 2025-07-15 23:15:03* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-07-15 23:35:42*/
class Solution {private boolean isYuan(char c) {return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';}public boolean isValid(String word) {if (word.length() < 3) {return false;}boolean hasYuan = false, hasFu = false;for (char c : word.toCharArray()) {if ('A' <= c && c <= 'Z') {c += 32;}if ('a' <= c && c <= 'z') {if (isYuan(c)) {hasYuan = true;} else {hasFu = true;}} else if (c < '0' || c > '9') {return false;}}return hasYuan && hasFu;}
}
Go
/** @Author: LetMeFly* @Date: 2025-07-15 23:15:03* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-07-15 23:40:26*/
package mainfunc isYuan3136(c byte) bool {return c == 'a' || c == 'e' || c == 'o' || c == 'i' || c == 'u'
}func isValid(word string) bool {if len(word) < 3 {return false}hasYuan, hasFu := false, falsefor _, c := range word {if 'A' <= c && c <= 'Z' {c += 32}if 'a' <= c && c <= 'z' {if isYuan3136(byte(c)) {hasYuan = true} else {hasFu = true}} else if c < '0' || c > '9' {return false}}return hasYuan && hasFu
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源