Leetcode 3136: Valid Word

grid47
grid47
Exploring patterns and algorithms
Dec 29, 2023 7 min read

You are given a string word. The word is considered valid if it satisfies the following conditions:

  1. It contains at least 3 characters.
  2. It contains only digits (0-9) and English letters (both uppercase and lowercase).
  3. It must include at least one vowel (‘a’, ’e’, ‘i’, ‘o’, ‘u’ and their uppercase counterparts).
  4. It must include at least one consonant (an English letter that is not a vowel).

Return true if the word is valid, otherwise return false.

Problem
Approach
Steps
Complexity
Input: You are given a string `word` containing English letters, digits, and special characters.
Example: Example 1: Input: word = "234Abcd" Output: true
Constraints:
• 1 <= word.length <= 20
• word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.
Output: Return `true` if the word satisfies the conditions for a valid word, otherwise return `false`.
Example: Example 1: Input: word = "234Abcd" Output: true
Constraints:
• The result must be either `true` or `false`.
Goal: The goal is to check if the word satisfies all the conditions for being a valid word. We need to check the length, the presence of vowels and consonants, and that the word contains only the allowed characters.
Steps:
• Check if the word length is at least 3.
• Check if the word contains only digits and English letters.
• Check for at least one vowel and at least one consonant in the word.
• Return `true` if all conditions are satisfied, otherwise return `false`.
Goal: The input constraints define the boundaries for the input word.
Steps:
• 1 <= word.length <= 20
• word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.
Assumptions:
• The word contains only the allowed characters (digits, letters, and specified special characters).
Input: Example 1:
Explanation: The word '234Abcd' satisfies the conditions: it has 7 characters, it contains a vowel ('A'), a consonant ('b'), and only valid characters (digits and letters).

Input: Example 2:
Explanation: The word 'a3' fails because it has fewer than 3 characters and does not contain a consonant.

Input: Example 3:
Explanation: The word 'a3$e' fails because it contains an invalid character ('$') and does not have a consonant.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus