Valid Word
EasyPrompt
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 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 <= 20wordconsists of English uppercase and lowercase letters, digits,'@','#', and'$'.
Approaches
2 approaches with complexity analysis and trade-offs.
This approach leverages a single, powerful regular expression to validate the word against all the given conditions simultaneously. Regular expressions provide a concise and declarative way to define patterns for string matching, making the code compact.
Algorithm
- Define a single regular expression string that combines all the rules: minimum length of 3, alphanumeric characters only, at least one vowel, and at least one consonant.
- Use the
String.matches()method with this regex on the inputword. - Return the boolean result of the
matches()method.
Walkthrough
The core of this approach is to construct a single regular expression that encapsulates all the validation rules. The final check is then a single call to word.matches(regex).
Here's a breakdown of the regex components:
^...$: These are anchors that ensure the entire string must match the pattern, not just a substring.{3,}: This is a quantifier that checks if the word's length is at least 3 characters.[a-zA-Z0-9]: This character class ensures that the word consists only of alphanumeric characters.(?=.*[aeiouAEIOU]): This is a positive lookahead. It asserts that somewhere in the string, there is at least one vowel (case-insensitive), without consuming any characters.(?=.*[[a-zA-Z]&&[^aeiouAEIOU]]): This is another positive lookahead that asserts the presence of at least one consonant. The character class[[a-zA-Z]&&[^aeiouAEIOU]]cleverly defines a consonant as any character that is an English letter but is not a vowel.
Combining these, we get a single regex that validates all conditions in one go.
Algorithm:
- Define a single regular expression string that combines all the rules: minimum length of 3, alphanumeric characters only, at least one vowel, and at least one consonant.
- Use the
String.matches()method with this regex on the inputword. - Return the boolean result of the
matches()method.
class Solution { public boolean isValid(String word) { // The regex combines all conditions: // - {3,} ensures length is at least 3. // - [a-zA-Z0-9] ensures only alphanumeric characters. // - (?=.*[aeiouAEIOU]) is a positive lookahead for at least one vowel. // - (?=.*[[a-zA-Z]&&[^aeiouAEIOU]]) is a positive lookahead for at least one consonant. // [[a-zA-Z]&&[^aeiouAEIOU]] is a character class intersection that matches any character // that is an alphabet but not a vowel. String regex = "^(?=.*[aeiouAEIOU])(?=.*[[a-zA-Z]&&[^aeiouAEIOU]])[a-zA-Z0-9]{3,}$"; return word.matches(regex); }}Complexity
Time
O(N), where N is the length of the word. The Java `matches` method, when using this kind of regex, typically involves a number of passes over the string proportional to the number of lookaheads, but it remains linear in time.
Space
O(1). The space used by the regex engine is related to the pattern's length, which is constant. No extra space proportional to the input string length is required.
Trade-offs
Pros
Very concise and declarative, expressing all validation rules in a single line of code.
Leverages the powerful built-in regex engine, avoiding manual iteration logic.
Cons
Regular expressions can be hard to read, understand, and debug, especially for those not familiar with the syntax.
May have performance overhead compared to a direct iterative approach, although this is negligible for small strings as per the constraints.
Solutions
Solution
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; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.