LeetCode 题解工作台

有效单词

有效单词 需要满足以下几个条件: 至少 包含 3 个字符。 由数字 0-9 和英文大小写字母组成。(不必包含所有这类字符。) 至少 包含一个 元音字母 。 至少 包含一个 辅音字母 。 给你一个字符串 word 。如果 word 是一个有效单词,则返回 true ,否则返回 false 。 注意: …

category

1

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · String-driven solution strategy

bolt

答案摘要

我们首先判断字符串的长度是否小于 3,如果是则返回 `false`。 然后我们遍历字符串,判断每个字符是否是字母或数字,如果不是则返回 `false`。否则我们判断该字符是否是元音字母,如果是则将 `has_vowel` 置为 `true`,否则将 `has_consonant` 置为 `true`。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 String-driven solution strategy 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

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

  • 至少 包含 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 由英文大写和小写字母、数字、'@''#''$' 组成。
lightbulb

解题思路

方法一:模拟

我们首先判断字符串的长度是否小于 3,如果是则返回 false

然后我们遍历字符串,判断每个字符是否是字母或数字,如果不是则返回 false。否则我们判断该字符是否是元音字母,如果是则将 has_vowel 置为 true,否则将 has_consonant 置为 true

最后,如果 has_vowelhas_consonant 都为 true,则返回 true,否则返回 false

时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。其中 nn 为字符串的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def isValid(self, word: str) -> bool:
        if len(word) < 3:
            return False
        has_vowel = has_consonant = False
        vs = set("aeiouAEIOU")
        for c in word:
            if not c.isalnum():
                return False
            if c.isalpha():
                if c in vs:
                    has_vowel = True
                else:
                    has_consonant = True
        return has_vowel and has_consonant
speed

复杂度分析

指标
时间complexity is O(n) since each character in the word is examined once. Space complexity is O(1) because only fixed counters for vowels, consonants, and flags are used regardless of word length.
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate efficiently iterates through the string without redundant passes.

  • question_mark

    Look for immediate handling of forbidden characters or early length validation.

  • question_mark

    Observe if the solution correctly tracks both vowels and consonants within a single loop.

warning

常见陷阱

外企场景
  • error

    Forgetting to verify the presence of at least one vowel and one consonant.

  • error

    Allowing invalid symbols like '$' or '#' to pass unnoticed.

  • error

    Not handling short strings below minimum length, leading to incorrect true returns.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Validate words with extended character sets including more special symbols.

  • arrow_right_alt

    Check words using a regex-based string-driven pattern for compactness.

  • arrow_right_alt

    Determine validity while ignoring case sensitivity for vowels and consonants.

help

常见问题

外企场景

有效单词题解:String-driven solution … | LeetCode #3136 简单