Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
A word is considered valid if:
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.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"
Output: false
Explanation:
This word contains a '$' character and does not have a consonant.
Constraints:
1 <= word.length <= 20word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"234Adas"
"b3"
"a3$e"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3136: Valid Word
class Solution {
public boolean isValid(String word) {
if (word.length() < 3) {
return false;
}
boolean hasVowel = false, hasConsonant = false;
boolean[] vs = new boolean[26];
for (char c : "aeiou".toCharArray()) {
vs[c - 'a'] = true;
}
for (char c : word.toCharArray()) {
if (Character.isAlphabetic(c)) {
if (vs[Character.toLowerCase(c) - 'a']) {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!Character.isDigit(c)) {
return false;
}
}
return hasVowel && hasConsonant;
}
}
// Accepted solution for LeetCode #3136: Valid Word
func isValid(word string) bool {
if len(word) < 3 {
return false
}
hasVowel := false
hasConsonant := false
vs := make([]bool, 26)
for _, c := range "aeiou" {
vs[c-'a'] = true
}
for _, c := range word {
if unicode.IsLetter(c) {
if vs[unicode.ToLower(c)-'a'] {
hasVowel = true
} else {
hasConsonant = true
}
} else if !unicode.IsDigit(c) {
return false
}
}
return hasVowel && hasConsonant
}
# Accepted solution for LeetCode #3136: Valid Word
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
// Accepted solution for LeetCode #3136: Valid Word
impl Solution {
pub fn is_valid(word: String) -> bool {
if word.len() < 3 {
return false;
}
let mut has_vowel = false;
let mut has_consonant = false;
let vowels = ['a', 'e', 'i', 'o', 'u'];
for c in word.chars() {
if !c.is_alphanumeric() {
return false;
}
if c.is_alphabetic() {
let lower_c = c.to_ascii_lowercase();
if vowels.contains(&lower_c) {
has_vowel = true;
} else {
has_consonant = true;
}
}
}
has_vowel && has_consonant
}
}
// Accepted solution for LeetCode #3136: Valid Word
function isValid(word: string): boolean {
if (word.length < 3) {
return false;
}
let hasVowel: boolean = false;
let hasConsonant: boolean = false;
const vowels: Set<string> = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
for (const c of word) {
if (!c.match(/[a-zA-Z0-9]/)) {
return false;
}
if (/[a-zA-Z]/.test(c)) {
if (vowels.has(c)) {
hasVowel = true;
} else {
hasConsonant = true;
}
}
}
return hasVowel && hasConsonant;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.