Solution-Valid Word
LeetCode DailyQuestion Solution (leetcode)Approach: One-Time Traversal
Intuition
First, we check whether the length of the given word is at least 3. Then, using a single traversal, we determine whether the word contains at least one vowel letter, at least one consonant letter, and only valid characters, i.e., letters and digits. Any other characters are not allowed.
Implementation###cpp
class Solution {
public:
bool isValid(string word) {
if (word.size() < 3) {
return false;
}
bool has_vowel = false;
bool has_consonant = false;
for (auto c : word) {
if (isalpha(c)) {
c = tolower(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
has_vowel = true;
} else {
has_consonant = true;
}
} else if (!isdigit(c)) {
return false;
}
}
return has_vowel && has_consonant;
}
};
###java
class Solution {
public boolean isValid(String word) {
if (word.length() < 3) {
return false;
}
boolean hasVowel = false;
boolean hasConsonant = false;
for (char c : word.toCharArray()) {
if (Character.isLetter(c)) {
char ch = Character.toLowerCase(c);
if (
ch == 'a' ||
ch == 'e' ||
ch == 'i' ||
ch == 'o' ||
ch == 'u'
) {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!Character.isDigit(c)) {
return false;
}
}
return hasVowel && hasConsonant;
}
}
###c
bool isValid(char* word) {
int len = strlen(word);
if (len < 3) {
return false;
}
bool has_vowel = false;
bool has_consonant = false;
for (int i = 0; i < len; i++) {
char c = word[i];
if (isalpha(c)) {
c = tolower(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
has_vowel = true;
} else {
has_consonant = true;
}
} else if (!isdigit(c)) {
return false;
}
}
return has_vowel && has_consonant;
}
###csharp
public class Solution {
public bool IsValid(string word) {
if (word.Length < 3) {
return false;
}
bool hasVowel = false;
bool hasConsonant = false;
foreach (char c in word) {
if (char.IsLetter(c)) {
char ch = char.ToLower(c);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' ||
ch == 'u') {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!char.IsDigit(c)) {
return false;
}
}
return hasVowel && hasConsonant;
}
}
###javascript
var isValid = function (word) {
if (word.length < 3) {
return false;
}
let hasVowel = false;
let hasConsonant = false;
for (const c of word) {
if (/[a-zA-Z]/.test(c)) {
const ch = c.toLowerCase();
if (
ch === "a" ||
ch === "e" ||
ch === "i" ||
ch === "o" ||
ch === "u"
) {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!/\d/.test(c)) {
return false;
}
}
return hasVowel && hasConsonant;
};
###golang
func isValid(word string) bool {
if len(word) < 3 {
return false
}
hasVowel := false
hasConsonant := false
for _, c := range word {
if unicode.IsLetter(c) {
ch := unicode.ToLower(c)
if ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' {
hasVowel = true
} else {
hasConsonant = true
}
} else if !unicode.IsDigit(c) {
return false
}
}
return hasVowel && hasConsonant
}
###python3
class Solution:
def isValid(self, word: str) -> bool:
if len(word) < 3:
return False
has_vowel = False
has_consonant = False
for c in word:
if c.isalpha():
if c.lower() in "aeiou":
has_vowel = True
else:
has_consonant = True
elif not c.isdigit():
return False
return has_vowel and has_consonant
###rust
impl Solution {
pub fn is_valid(word: String) -> bool {
if word.len() < 3 {
return false;
}
let mut has_vowel = false;
let mut has_consonant = false;
for c in word.chars() {
if c.is_ascii_alphabetic() {
let lc = c.to_ascii_lowercase();
if "aeiou".contains(lc) {
has_vowel = true;
} else {
has_consonant = true;
}
} else if !c.is_ascii_digit() {
return false;
}
}
has_vowel && has_consonant
}
}
###typescript
function isValid(word: string): boolean {
if (word.length < 3) {
return false;
}
let hasVowel = false;
let hasConsonant = false;
for (const c of word) {
if (/[a-zA-Z]/.test(c)) {
const ch = c.toLowerCase();
if (
ch === "a" ||
ch === "e" ||
ch === "i" ||
ch === "o" ||
ch === "u"
) {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!/\d/.test(c)) {
return false;
}
}
return hasVowel && hasConsonant;
}
Complexity analysis
Let $n$ be the length of $\textit{word}$.
- Time complexity: $O(n)$. We iterate through the word only once to check each character, so the time complexity is linear in the length of the word.
- Space complexity: $O(1)$.
Generated by RSStT. The copyright belongs to the original author.