Solving Leetcode Interviews in Seconds with AI: Valid Word
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3136" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
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 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 '$'.
Explanation
- The solution checks if the word's length is at least 3. Then, it iterates through the word to verify that all characters are either digits or English letters. Concurrently, it identifies the presence of at least one vowel and one consonant.
- The solution short-circuits and return
Falseif a character doesn't belong to the accepted characters or if after the entire word has been parsed, the counters for vowel and consonant are still zero. - Runtime complexity: O(n), where n is the length of the word. Storage complexity: O(1).
- The solution short-circuits and return
Code
def isValid(word: str) -> bool:
"""
Given a string word, return true if word is valid, otherwise, return false.
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.
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 '$'.
"""
if len(word) < 3:
return False
has_vowel = False
has_consonant = False
for char in word:
if not ('0' <= char <= '9' or 'a' <= char <= 'z' or 'A' <= char <= 'Z'):
return False
if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
if char in 'aeiouAEIOU':
has_vowel = True
else:
has_consonant = True
return has_vowel and has_consonant