闯关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

相关推荐
科大饭桶36 分钟前
数据结构自学Day15 -- 非比较排序--计数排序
数据结构·算法·leetcode·排序算法·c
JNU freshman40 分钟前
C++ 常用的数据结构(适配器容量:栈、队列、优先队列)
数据结构·c++
程序员编程指南42 分钟前
Qt字符串处理与正则表达式应用
c语言·c++·qt·正则表达式
BS_Li1 小时前
C++模板进阶
c++·模板进阶
剪一朵云爱着1 小时前
力扣二叉树的前序中序后序遍历总结
算法·leetcode·二叉树
szx04271 小时前
缓存HDC内容用于后续Direct2D绘制.
c++·缓存·directx·d2d·direct2d·dx·gdi+dx
橘颂TA1 小时前
【C++】C++11特性的介绍和使用(第三篇)
前端·c++·算法·c++11
嶔某1 小时前
网络:应用层
linux·服务器·网络·c++
linux kernel2 小时前
第十一讲:模板进阶以及反向迭代器
c++
技术卷5 小时前
详解力扣高频 SQL 50 题之584. 寻找用户推荐人【入门】
sql·leetcode·oracle