feat: add solutions to lc problem: No.3136 (#4569)

No.3136.Valid Word
This commit is contained in:
Libin YANG 2025-07-15 06:55:28 +08:00 committed by GitHub
parent a666697e04
commit 6f7b0b3951
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 185 additions and 0 deletions

View File

@ -235,6 +235,71 @@ function isValid(word: string): boolean {
}
```
#### Rust
```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;
let vowels = ['a', 'e', 'i', 'o', 'u'];
for c in word.chars() {
if !c.is_alphanumeric() {
return false;
}
if c.is_alphabetic() {
let lower_c = c.to_ascii_lowercase();
if vowels.contains(&lower_c) {
has_vowel = true;
} else {
has_consonant = true;
}
}
}
has_vowel && has_consonant
}
}
```
#### C#
```cs
public class Solution {
public bool IsValid(string word) {
if (word.Length < 3) {
return false;
}
bool hasVowel = false, hasConsonant = false;
bool[] vs = new bool[26];
foreach (char c in "aeiou") {
vs[c - 'a'] = true;
}
foreach (char c in word) {
if (char.IsLetter(c)) {
char lower = char.ToLower(c);
if (vs[lower - 'a']) {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!char.IsDigit(c)) {
return false;
}
}
return hasVowel && hasConsonant;
}
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -235,6 +235,71 @@ function isValid(word: string): boolean {
}
```
#### Rust
```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;
let vowels = ['a', 'e', 'i', 'o', 'u'];
for c in word.chars() {
if !c.is_alphanumeric() {
return false;
}
if c.is_alphabetic() {
let lower_c = c.to_ascii_lowercase();
if vowels.contains(&lower_c) {
has_vowel = true;
} else {
has_consonant = true;
}
}
}
has_vowel && has_consonant
}
}
```
#### C#
```cs
public class Solution {
public bool IsValid(string word) {
if (word.Length < 3) {
return false;
}
bool hasVowel = false, hasConsonant = false;
bool[] vs = new bool[26];
foreach (char c in "aeiou") {
vs[c - 'a'] = true;
}
foreach (char c in word) {
if (char.IsLetter(c)) {
char lower = char.ToLower(c);
if (vs[lower - 'a']) {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!char.IsDigit(c)) {
return false;
}
}
return hasVowel && hasConsonant;
}
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -0,0 +1,28 @@
public class Solution {
public bool IsValid(string word) {
if (word.Length < 3) {
return false;
}
bool hasVowel = false, hasConsonant = false;
bool[] vs = new bool[26];
foreach (char c in "aeiou") {
vs[c - 'a'] = true;
}
foreach (char c in word) {
if (char.IsLetter(c)) {
char lower = char.ToLower(c);
if (vs[lower - 'a']) {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!char.IsDigit(c)) {
return false;
}
}
return hasVowel && hasConsonant;
}
}

View File

@ -0,0 +1,27 @@
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;
let vowels = ['a', 'e', 'i', 'o', 'u'];
for c in word.chars() {
if !c.is_alphanumeric() {
return false;
}
if c.is_alphabetic() {
let lower_c = c.to_ascii_lowercase();
if vowels.contains(&lower_c) {
has_vowel = true;
} else {
has_consonant = true;
}
}
}
has_vowel && has_consonant
}
}