闯关leetcode——3136. Valid Word

大纲

题目

地址

https://leetcode.com/problems/valid-word/description/

内容

A word is considered valid if:

  • It contains a minimum of 3 characters.
  • It contains only digits (0-9), and English letters (uppercase and lowercase).
  • It includes at least one vowel.
  • It includes at least one consonant.

You are given a string word.

Return true if word is valid, otherwise, return false.

Notes:

  • 'a', 'e', 'i', 'o', 'u', and their uppercases are vowels.
  • A consonant is an English letter that is not a vowel.

Example 1:

Input: word = "234Adas"

Output: true

Explanation:

This word satisfies the conditions.

Example 2:

Input: word = "b3"

Output: false

Explanation:

The length of this word is fewer than 3, and does not have a vowel.

Example 3:

Input: word = "a3 e " O u t p u t : f a l s e E x p l a n a t i o n : T h i s w o r d c o n t a i n s a ′ e" Output: false Explanation: This word contains a ' e"Output:falseExplanation:Thiswordcontainsa′' character and does not have a consonant.

Constraints:

  • 1 <= word.length <= 20
  • word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.

解题

这题要检测一个数是否符合以下特点:

  • 至少包含3个字符
  • 只能包含大小写字母和数字
  • 至少有一个a、e、i、o、u大写或小写的字母
  • 至少包含一个非a、e、i、o、u大写或小写的字母

解法也很简单,按这些条件把规则写好即可。

cpp 复制代码
#include <string>
using namespace std;

class Solution {
public:
    bool isValid(string word) {
        if (word.size() < 3) {
            return false;
        }
        bool vowel = false;
        bool consonant = false;
        for (char c : word) {
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
            || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
                vowel = true;
            } else if ( (c >= 'a' && c <= 'z')
                || (c >= 'A' && c <= 'Z'))
            {
                consonant = true;
            } 
            else if (c >= '0' && c <= '9') {
                continue;
            }
            else {
                return false;
            }
        }
        return vowel && consonant;
    }
};

代码地址

https://github.com/f304646673/leetcode/tree/main/3136-Valid-Word/cplusplus

相关推荐
二进制人工智能37 分钟前
【QT5 网络编程示例】TCP 通信
网络·c++·qt·tcp/ip
莫有杯子的龙潭峡谷3 小时前
3.31 代码随想录第三十一天打卡
c++·算法
LuckyLay3 小时前
LeetCode算法题(Go语言实现)_22
算法·leetcode·golang
AaronZZH4 小时前
【进阶】vscode 中使用 cmake 编译调试 C++ 工程
c++·ide·vscode
杨筱毅4 小时前
【性能优化点滴】odygrd/quill 中将 MacroMetadata 变量声明为 constexpr
c++·性能优化
NaZiMeKiY4 小时前
C++ 结构体与函数
开发语言·c++
涛ing4 小时前
【Git “fetch“ 命令详解】
linux·c语言·c++·人工智能·git·vscode·svn
学习是种信仰啊5 小时前
QT图片轮播器实现方法二(QT实操2)
开发语言·c++·qt
nqqcat~6 小时前
STL常用算法
开发语言·c++·算法
_GR7 小时前
2022年蓝桥杯第十三届C&C++大学B组真题及代码
c语言·数据结构·c++·算法·蓝桥杯·动态规划