Valid Word - Complete Solution Guide
Valid Word is LeetCode problem 3136, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks you to determine if a given word is 'valid' according to specific rules. A valid word must meet four criteria: 1. **Minimum Length:** It must contain at least 3 characters. 2. **Character Set:** It can only contain digits (0-9) and English letters (a-z, A-Z). 3. **Vowel Presence:** It must include at least one vowel (a, e, i, o, u, and their uppercase versions). 4. **Consonant Presence:** It must include at least one consonant (any letter that is not a vowel). The input is a single string `word`. The output is a boolean value: `true` if the word is valid, and `false` otherwise.
Solution Approach
The solution uses a single loop to iterate over each character in the input string. It first checks the length of the word. If it's less than 3, it immediately returns `false`. Otherwise, it initializes two boolean flags, `hasVowel` and `hasConsonant`, to `false`. Inside the loop, it first verifies if the current character is alphanumeric. If not, it returns `false`. If it is alphanumeric, it checks if it's a vowel and updates `hasVowel` accordingly. If it's a letter and not a vowel, it updates `hasConsonant`. Finally, it returns `true` only if both `hasVowel` and `hasConsonant` are `true`, indicating that the word meets all criteria.
Step-by-Step Algorithm
- Check if the length of the word is less than 3. If so, return `false`.
- Initialize boolean variables `hasVowel` and `hasConsonant` to `false`.
- Iterate through each character in the word:
- Check if the character is alphanumeric. If not, return `false`.
- If the character is a vowel, set `hasVowel` to `true`.
- If the character is a consonant, set `hasConsonant` to `true`.
- After the loop, return `true` if both `hasVowel` and `hasConsonant` are `true`; otherwise, return `false`.
Key Insights
- Iterating through the word character by character allows checking for each validation rule simultaneously.
- Using boolean flags (`hasVowel`, `hasConsonant`) efficiently tracks the presence of vowels and consonants.
- Early exit strategies (returning `false` immediately upon encountering an invalid character) optimize the solution.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String.
Companies
Asked at: Expedia, UKG.