LeetCode Problem Workspace

Valid Word

Determine if a given string qualifies as a valid word using a string-driven solution pattern with explicit character rules.

category

1

Topics

code_blocks

7

Code langs

hub

3

Related

Practice Focus

Easy · String-driven solution strategy

bolt

Answer-first summary

Determine if a given string qualifies as a valid word using a string-driven solution pattern with explicit character rules.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for String-driven solution strategy

Try AiBox Copilotarrow_forward

The solution immediately checks all character rules using a string-driven approach. Iterate through each character of the word, validating letters, digits, and special symbols. Return true only if the word meets the length requirement, contains at least one vowel and one consonant, and has no invalid characters.

Problem Statement

You are given a string word consisting of English letters, digits, and special symbols '@', '#', and '$'. A word is considered valid if it meets specific character rules including length, vowel and consonant presence, and allowed characters.

Return true if the word is valid according to these rules, otherwise return false. Carefully consider string length, character types, and ensure no forbidden symbols appear while checking each character sequentially.

Examples

Example 1

Input: word = "234Adas"

Output: true

This word satisfies the conditions.

Example 2

Input: word = "b3"

Output: false

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

Example 3

Input: word = "a3$e"

Output: false

This word contains a '$' character and does not have a consonant.

Constraints

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

Solution Approach

Iterate and Validate Characters

Loop through each character in the word and check if it is an allowed letter, digit, or symbol. Track the presence of at least one vowel and one consonant to satisfy the validity rules.

Check Length and Early Exit

Immediately return false if the word length is less than 3. This prevents unnecessary checks for words that automatically fail the pattern rules.

Return Final Validation Result

After processing all characters, return true if all conditions are satisfied: correct length, allowed characters only, at least one vowel, and at least one consonant. Otherwise, return false.

Complexity Analysis

Metric Value
Time O(n)
Space O(1)

Time 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.

What Interviewers Usually Probe

  • Check if the candidate efficiently iterates through the string without redundant passes.
  • Look for immediate handling of forbidden characters or early length validation.
  • Observe if the solution correctly tracks both vowels and consonants within a single loop.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to verify the presence of at least one vowel and one consonant.
  • Allowing invalid symbols like '$' or '#' to pass unnoticed.
  • Not handling short strings below minimum length, leading to incorrect true returns.

Follow-up variants

  • Validate words with extended character sets including more special symbols.
  • Check words using a regex-based string-driven pattern for compactness.
  • Determine validity while ignoring case sensitivity for vowels and consonants.

FAQ

What is the main pattern used to solve Valid Word?

The problem follows a string-driven solution pattern where each character is validated sequentially with explicit rules.

What characters are allowed in a valid word?

English letters, digits, and the special symbols '@', '#', and '$' are allowed, but at least one vowel and consonant are required.

Can a short word be considered valid?

No, words shorter than 3 characters automatically fail the validity rules.

What is the time complexity of the solution?

The solution runs in O(n) time since it iterates through each character once.

Does GhostInterview assist with handling invalid characters?

Yes, it flags forbidden symbols immediately and guides the solution to enforce correct character rules.

terminal

Solution

Solution 1: Simulation

First, we check if the length of the string is less than 3. If it is, we return `false`.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
Valid Word Solution: String-driven solution strategy | LeetCode #3136 Easy