力扣25.7.15每日一题——有效单词
Description
应该都能看懂吧……
Solution
一道简单的模拟题。
按照题意枚举字符串,判断元/辅音;判断合法即可。
也不知道今天的题为什么怎么淼
Code(C++、Python3)
C++
class Solution {
public:bool isValid(string word) {if (word.size() < 3) return false;int e = 0, f = 0;for (char c : word) {if (isalpha(c)) {c = tolower(c);if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {e++;} else {f++;}} else if (!isdigit(c)){return false;}}return e && f;}
};
Python3
class Solution:def isValid(self, word: str) -> bool:if len(word) < 3:return Falsee = f = Falsefor c in word:if c.isalpha():if (c.lower() in 'aeiou'):e = Trueelse :f = Trueelif not c.isdigit():return Falsereturn e and f
欢迎大家关注LeetCode——C2h6oqwq。也恳求大家点赞收藏加关注~~~