LeetCode 3136: Valid Word Solution
Master LeetCode problem 3136 (Valid Word), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3136. Valid Word
Problem Explanation
Explanation
To solve this problem, we can iterate through the characters of the given word and keep track of the counts of vowels, consonants, and total characters. We also need to ensure that the word contains a minimum of 3 characters. At the end, we check if all conditions are satisfied to determine if the word is valid.
- Initialize counts for vowels, consonants, and total characters.
- Iterate through each character in the word:
- Check if the character is a vowel or consonant, increment respective counts.
- Increment the total character count.
- Check if the total character count is at least 3 and there is at least one vowel and one consonant.
- Return true if all conditions are met, otherwise return false.
Time Complexity: O(n) where n is the length of the word
Space Complexity: O(1)
Solution Code
class Solution {
public boolean isValid(String word) {
if (word.length() < 3) {
return false;
}
int vowels = 0;
int consonants = 0;
for (char c : word.toCharArray()) {
if (Character.isLetter(c)) {
if ("aeiouAEIOU".indexOf(c) != -1) {
vowels++;
} else {
consonants++;
}
}
}
return vowels > 0 && consonants > 0;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3136 (Valid Word)?
This page provides optimized solutions for LeetCode problem 3136 (Valid Word) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3136 (Valid Word)?
The time complexity for LeetCode 3136 (Valid Word) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3136 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3136 in Java, C++, or Python.