当前位置: 首页 > news >正文

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和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

http://www.dtcms.com/a/280858.html

相关文章:

  • [实战] 基8 FFT/IFFT算法原理与实现(完整C代码)
  • 【每天一个知识点】多模态信息(Multimodal Information)
  • 【知识扫盲】tokenizer.json中的vocab和merges是什么?
  • 【机器学习】第二章 Python入门
  • 【Unity】MiniGame编辑器小游戏(十四)基础支持模块(游戏窗口、游戏对象、物理系统、动画系统、射线检测)
  • 数学中的教学思想
  • MySQL 8.0 OCP 1Z0-908 题目解析(24)
  • P3842 [TJOI2007] 线段
  • Sharding-JDBC 分布式事务实战指南:XA/Seata 方案解析
  • sqli-labs靶场通关笔记:第18-19关 HTTP头部注入
  • 【C++】初识C++(1)
  • 课题学习笔记1——文本问答与信息抽取关键技术研究论文阅读(用于无结构化文本问答的文本生成技术)
  • Java 大视界 -- Java 大数据机器学习模型在金融风险传染路径分析与防控策略制定中的应用(347)
  • QT——QList的详细讲解
  • Redis的下载安装+基础操作+redis客户端的安装
  • 使用 1Panel PHP 运行环境部署 WordPress
  • 辨析git reset三种模式以及和git revert的区别:回退到指定版本和撤销指定版本的操作
  • 零样本轴承故障诊断SC - GAN模型
  • 【PCIe 总线及设备入门学习专栏 5.1.2 -- PCIe EP core_rst_n 与 app_rst_n】
  • React-router
  • 未来大模型在中小型企业如何实现普及
  • PG备份一(逻辑备份)
  • Kafka——生产者消息分区机制原理剖析
  • Java基础教程(009): Java 的封装
  • Samba配置使用
  • 算法学习笔记:23.贪心算法之活动选择问题 ——从原理到实战,涵盖 LeetCode 与考研 408 例题
  • 重学前端005 --- 响应式网页设计 CSS 盒子模型
  • Python函数进阶
  • python 基于 httpx 的流式请求
  • 封装---统一处理接口与打印错误信息