3136. Valid Word LeetCode Solution

In this guide, you will get 3136. Valid Word LeetCode Solution with the best time and space complexity. The solution to Valid Word problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Valid Word solution in C++
  4. Valid Word solution in Java
  5. Valid Word solution in Python
  6. Additional Resources
3136. Valid Word LeetCode Solution image

Problem Statement of Valid Word

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 <= 20
word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3136. Valid Word LeetCode Solution in C++

class Solution {
 public:
  bool isValid(string word) {
    return word.length() >= 3 &&
           ranges::all_of(word, [](char c) { return isalnum(c); }) &&
           ranges::any_of(word, isVowel) && ranges::any_of(word, isConsonant);
  }

 private:
  static bool isVowel(char c) {
    static constexpr string_view kVowels = "aeiouAEIOU";
    return kVowels.find(c) != string_view::npos;
  }

  static bool isConsonant(char c) {
    return isalpha(c) && !isVowel(c);
  }
};
/* code provided by PROGIEZ */

3136. Valid Word LeetCode Solution in Java

class Solution {
  public boolean isValid(String word) {
    return word.length() >= 3 && word.chars().allMatch(Character::isLetterOrDigit) &&
        word.chars().anyMatch(c -> isVowel((char) c)) &&
        word.chars().anyMatch(c -> isConsonant((char) c));
  }

  private boolean isVowel(char c) {
    return "aeiouAEIOU".indexOf(c) != -1;
  }

  private boolean isConsonant(char c) {
    return Character.isLetter(c) && !isVowel(c);
  }
}
// code provided by PROGIEZ

3136. Valid Word LeetCode Solution in Python

class Solution:
  def isValid(self, word: str) -> bool:
    kVowels = 'aeiouAEIOU'

    def isConsonant(c: str) -> bool:
      return c.isalpha() and c not in kVowels

    return (len(word) >= 3 and
            all(c.isalnum() for c in word) and
            any(c in kVowels for c in word) and
            any(isConsonant(c) for c in word))
# code by PROGIEZ

Additional Resources

See also  854. K-Similar Strings LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.